OneToOne中的未知mappedBy

时间:2017-10-08 19:01:19

标签: java hibernate one-to-one

当我想要与tabble的OneToOne关系时:NewUser to CreateBill我得到了异常: Exception in thread "main" org.hibernate.AnnotationException: Unknown mappedBy in: domain.CreateBill.user, referenced property unknown: domain.NewUser.user

在交换机中,无论​​我是否点击了E' E'或者' N'#39;我有这个例外...

使用NewUser的第一堂课,主要是获取/设置

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="Urzytkownik")
public class NewUser {

    @Id
    @GeneratedValue
    private long id;

    private String name;
    private String surName;
    private int pesel; // s08
    private int telephoneNumber;
    private int pin;

    @OneToOne
    private CreateBill bill;


    public int getPin() {
        return pin;
    }

    public void setPin(int pin) {
        this.pin = pin;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public int getPesel() {
        return pesel;
    }

    public void setPesel(int pesel) {
        this.pesel = pesel;
    }

    public int getTelephoneNumber() {
        return telephoneNumber;
    }

    public void setTelephoneNumber(int telephoneNumber) {
        this.telephoneNumber = telephoneNumber;
    }


    public CreateBill getBill() {
        return bill;
    }

    public void setBill(CreateBill bill) {
        this.bill = bill;
    }   }

使用CreateBill的第二课,主要是获取/设置和我最糟糕的问题:     @OneToOne(mappedBy =" user"):X

package domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="Rachunek")
public class CreateBill {

    @Id
    @GeneratedValue
    private int Id;

    private int IBAN;
    private int money;

    @OneToOne(mappedBy="user")
    private NewUser user;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public int getIBAN() {
        return IBAN;
    }

    public void setIBAN(int iBAN) {
        IBAN = iBAN;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public NewUser getUser() {
        return user;
    }

    public void setUser(NewUser user) {
        this.user = user;
    }   }


package domain;

import java.util.Scanner;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class CreateNewUserBill {

    public void CN() {

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("manager1");
        EntityManager entityManager = entityManagerFactory.createEntityManager();


        NewUser user = new NewUser();
        CreateBill bill = new CreateBill();


        System.out.println("Please enter name: ");
        Scanner scannerName = new Scanner(System.in);
        String SNa = scannerName.nextLine();

        System.out.println("Please enter surname: ");
        Scanner scannerSurName = new Scanner(System.in);
        String SSN = scannerSurName.nextLine();

        System.out.println("Please enter pesel: ");
        Scanner scannerPesel = new Scanner(System.in);
        int SPe = scannerPesel.nextInt();

        System.out.println("Please enter telephone number: ");
        Scanner scannerTelephoneNumber = new Scanner(System.in);
        int STN = scannerTelephoneNumber.nextInt();

        System.out.println("Please enter new PIN: ");
        Scanner scannerPIN = new Scanner(System.in);
        int SPi = scannerPIN.nextInt();


        System.out.println("Please enter IBAN: ");
        Scanner scannerIBAN = new Scanner(System.in);
        int SIb = scannerIBAN.nextInt();

        System.out.println("Please enter Money: ");
        Scanner scannerMoney = new Scanner(System.in);
        int SMo = scannerMoney.nextInt();



        user.setName(SNa);
        user.setSurName(SSN);
        user.setPesel(SPe);
        user.setTelephoneNumber(STN);
        user.setPin(SPi);
        bill.setIBAN(SIb);
        bill.setMoney(SMo);
        user.setBill(bill);


        entityManager.getTransaction().begin();
        entityManager.persist(user);
        entityManager.persist(bill);
        entityManager.getTransaction().commit();

        entityManager.close();
        entityManagerFactory.close();

    }

}

午餐课。 最重要的是:' N'和' P'。

package domain;

import java.util.Scanner;

public class ATM {
    private Scanner scanner;

    public void changer() {

        int PIN;
        char switcher;

        System.out.println("Type N to create account");
        System.out.println("Type P to enter PIN");
        System.out.println("Type E to exit");
        System.out.println("Type H to view HELP");
        System.out.println();
        System.out.println();

        scanner = new Scanner(System.in);
        switcher = scanner.next().charAt(0);

        switch (switcher) {
        case 'N':

            CreateNewUserBill user = new CreateNewUserBill();
            user.CN();

            break;

        case 'E':
            System.out.println("Bye!");
            break;

        case 'H':
            System.out.println("If you have any problem please visit us branch bank.");
            break;

        case 'P':
            System.out.println("Enter PIN");

            Scanner odczyt = new Scanner(System.in);
            PIN = odczyt.nextInt();
            VerificationPIN v = new VerificationPIN();

            while (PIN != v.verification()) {
                System.out.println("ERROR PIN, write it again");
                PIN = odczyt.nextInt();
            }
            System.out.print("PIN OK");


        default:
            System.out.println("ERROR 404");
        }

    }

}

package domain;


public class Main {

    public static void main(String[] args) {
    ATM atm = new ATM();
        atm.changer();


    }

}

persistance.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.user" value="atm" />
        <property name="javax.persistence.jdbc.password" value="password1" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mojaBaza" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="create" /> 
    </properties>
</persistence-unit>

1 个答案:

答案 0 :(得分:2)

mappedBy值应引用所有者类的属性名称。 user类中没有NewUser属性。在你的情况下它应该是

@OneToOne(mappedBy = "bill")
private NewUser user;

有关文档,请参阅http://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html