OneToOne 中的未知 mappedBy

Unknown mappedBy in OneToOne

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

在 switch 中,无论我点击 'E' 还是 'N' 我都得到了这个异常...

首先class与NewUser,主要是get/set

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;
    }   }

第二个 class 使用 CreateBill,主要是 get/set 和我最糟糕的问题: @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();

    }

}

午餐class。 最重要的是:'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>

mappedBy 值应引用 属性 所有者的姓名 class。 NewUserclass中没有user属性。在你的情况下应该是

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

参考 http://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html 文档。