hibernate映射异常未知的mappedBy

时间:2017-10-08 14:20:50

标签: hibernate exception

大家好我得到这个错误我做错了什么? 这些是我的课程: 这是我试图运行时得到的错误

线程“main”中的异常org.hibernate.AnnotationException:未知的mappedBy in:hiberbiber.Student.studendetails,引用的属性unknown:hiberbiber.StudenDetails.student

package hiberbiber;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;


@Entity
@Table(name = "STUDENTDETAILS")
public class StudenDetails {

private int id;
private String jmbg;
private int godine;
Student student ;
public StudenDetails() {
}

public StudenDetails(String jmbg, int godine) {
    this.jmbg = jmbg;
    this.godine = godine;
}

public StudenDetails(int id, String jmbg, int godine, Student student) {
    this.id = id;
    this.jmbg = jmbg;
    this.godine = godine;
    this.student = student;
}

@Id
@GeneratedValue
public int getId() {
    return id;
}
@OneToOne(fetch = FetchType.LAZY,mappedBy = "student",cascade = 
CascadeType.ALL)
@PrimaryKeyJoinColumn
public Student getStudent() {
    return student;
}

public void setStudent(Student student) {
    this.student = student;
}

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

public String getJmbg() {
    return jmbg;
}

public void setJmbg(String jmbg) {
    this.jmbg = jmbg;
}

public int getGodine() {
    return godine;
}

public void setGodine(int godine) {
    this.godine = godine;
}

}
    package hiberbiber;

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

   @Entity
   @Table(name = "PHONE")
   public class Phone {

   private long phoneId;
   private String phoneType;
   private String phoneNumber;

   public Phone() {
   }

   public Phone(String phoneType, String phoneNumber) {
       this.phoneType = phoneType;
       this.phoneNumber = phoneNumber;
   }

   @Id
   @GeneratedValue
   @Column(name = "PHONE_ID")
   public long getPhoneId() {
        return this.phoneId;
   }

   public void setPhoneId(long phoneId) {
        this.phoneId = phoneId;

   }

   @Column(name = "PHONE_TYPE", nullable = false, length = 10)
   public String getPhoneType() {
       return this.phoneType;
   }

   public void setPhoneType(String phoneType) {
       this.phoneType = phoneType;
   }

   @Column(name = "PHONE_NUMBER", nullable = false, length = 15)
   public String getPhoneNumber() {
       return this.phoneNumber;
   }

   public void setPhoneNumber(String phoneNumber) {
       this.phoneNumber = phoneNumber;
      }
   }

和第三个

package hiberbiber;

import java.io.Serializable;
import java.security.Identity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Table(name = "STUDENT")
public class Student implements Serializable {

private long studentId;
private String studentName;
private Set<Phone> studentPhoneNumbers = new HashSet<>(0);

private StudenDetails studendetails;
public Student() {
}

public Student(String studentName, Set<Phone> studentPhoneNumbers) {
    this.studentName = studentName;
    this.studentPhoneNumbers = studentPhoneNumbers;
}

@Id
@GeneratedValue() 
@Column(name = "STUDENT_ID")
public long getStudentId() {
    return this.studentId;
}


@OneToOne(fetch = FetchType.LAZY,mappedBy = "student",cascade = CascadeType.ALL)
public StudenDetails getStudendetails() {
    return studendetails;
}



public void setStudendetails(StudenDetails studendetails) {
    this.studendetails = studendetails;
}

public void setStudentId(long studentId) {
    this.studentId = studentId;
}

public Student(long studentId, String studentName, StudenDetails studendetails) {
    this.studentId = studentId;
    this.studentName = studentName;
    this.studendetails = studendetails;
}

@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName() {
    return this.studentName;
}

public void setStudentName(String studentName) {
    this.studentName = studentName;
}

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_PHONE", joinColumns = {
    @JoinColumn(name = "STUDENT_ID")}, inverseJoinColumns = {
    @JoinColumn(name = "PHONE_ID")})
public Set<Phone> getStudentPhoneNumbers() {
    return this.studentPhoneNumbers;
}

public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) {
    this.studentPhoneNumbers = studentPhoneNumbers;
}
}

这是我尝试从

运行的主要类
public class HiberBiber {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   Session session = HibernateUtil.getFactory().openSession();
       Transaction tx = null;
        try {
             tx = session.beginTransaction();

             Set<Phone> phoneNumbers = new HashSet<Phone>();

             phoneNumbers.add(new Phone("house", "32333"));
             phoneNumbers.add(new Phone("mobile", "06432333"));


        Student student = new Student("eswar", phoneNumbers);

        StudenDetails sd = new StudenDetails("123123", 13);
        sd.setStudent(student);

        student.setStudendetails(sd);


        session.persist(student);
        tx.commit();
         } catch (HibernateException e) {
            tx.rollback();
         } finally{
            session.close();
        }

    }

}

2 个答案:

答案 0 :(得分:0)

您在关系的两边都有mappedBy。您只能将其设置在您认为属于自有方的一侧:

@OneToOne(fetch = FetchType.LAZY,cascade = 
CascadeType.ALL)
@PrimaryKeyJoinColumn
public Student getStudent() {
    return student;
}

@OneToOne(fetch = FetchType.LAZY, mappedBy="student",cascade = CascadeType.ALL)
public StudenDetails getStudendetails() {
    return studendetails;
}

答案 1 :(得分:0)

您正在使用双方的映射。它需要设置在一边。在StudentDetails中使用Student的映射:

@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Student getStudent() {
    return student;
}