线程“主” org.hibernate.AnnotationException中的异常:@OneToOne或@ManyToOne

时间:2019-03-18 17:43:21

标签: java mysql hibernate

您好,我刚刚创建了一个应用程序,该应用程序通过一对一映射将数据保存在表中

onetoone database contains table instructor and instructor_detail 每当我尝试将数据保存在表中时,都会出现以下错误。

`Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.mapping.onetoone.Instructor.theInstructorDetail references an unknown entity: com.mapping.onetoone.InstructorDetail`

这是我的带有@Entity和OneToOne批注的代码 Instructor.java

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="email")
private String email;

@OneToOne(cascade=CascadeType.ALL   )
@JoinColumn(name="instructor_detail_id")
private InstructorDetail theInstructorDetail;

public Instructor(){}

public Instructor(String firstName, String lastName, String email) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}

public int getId() {
    return id;
}

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

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public InstructorDetail getTheInstructorDetail() {
    return theInstructorDetail;
}

public void setTheInstructorDetail(InstructorDetail theInstructorDetail) {
    this.theInstructorDetail = theInstructorDetail;
}

@Override
public String toString() {
    return "Instructor [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
            + ", theInstructorDetail=" + theInstructorDetail + "]";
}

这是我的 InstructorDetail.java

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="youtube_channel")
private String youTubeChannel;

@Column(name="hobby")
private String hobby;

public InstructorDetail(){}

public InstructorDetail(String youTubeChannel, String hobby) {
    super();
    this.youTubeChannel = youTubeChannel;
    this.hobby = hobby;
}

public int getId() {
    return id;
}

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

public String getYouTubeChannel() {
    return youTubeChannel;
}

public void setYouTubeChannel(String youTubeChannel) {
    this.youTubeChannel = youTubeChannel;
}

public String getHobby() {
    return hobby;
}

public void setHobby(String hobby) {
    this.hobby = hobby;
}

@Override
public String toString() {
    return "InstructorDetail [id=" + id + ", youTubeChannel=" + youTubeChannel + ", hobby=" + hobby + "]";
}

CreateInstructor.java

public static void main(String[] args) {

    SessionFactory factory=new Configuration().configure("hibernate1.cfg.xml")
            .addAnnotatedClass(Instructor.class).buildSessionFactory();
    Session session = factory.getCurrentSession();
    try
    {
        System.out.println("Object created. Now creating Instructor object...");
        Instructor ins=new Instructor("elon", "musk", "elonmusk@hotmail.com");

        System.out.println("Creating InstructorDetail object...");
        InstructorDetail theInstructorDetail=new InstructorDetail("vella Panti Adda", "Acting");

        ins.setTheInstructorDetail(theInstructorDetail);

        session.beginTransaction();
        System.out.println("Saving data....");

        session.save(ins);
        session.getTransaction().commit();

        System.out.println("Data saved!");

    }
    finally
    {
        factory.close();
    }
}

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

您已经尝试过了。

public Instructor(String firstName, String lastName, String email) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.theInstructorDetail= new InstructorDetail();
}

我认为您应该开始所有属性。

答案 1 :(得分:0)

OneToOne单向关联

您的代码设置指向OneToOne Unidirectional Association,其中InstructorDetail为父级,Instructor为子级关联。为此,在保存/保存Instructor实体之前,应保存/保存InstructorDetail。

session.beginTransaction();
session.save(theInstructorDetail);
session.save(ins);
session.getTransaction().commit();

OneToOne双向关联

如果您不想在数据库中关联FK,请在Java hibernate中创建双向关联:

Instructor.java

@OneToOne
 @JoinColumn(name = "instructor_detail_id")
 private InstructorDetail theInstructorDetail;

InstructorDetail.java

 @OneToOne(mappedBy="instructor_detail")
 private Instructor theInstructor;

坚持逻辑

Instructor ins=new Instructor("elon", "musk", "elonmusk@hotmail.com");
InstructorDetail theInstructorDetail=new InstructorDetail("vella Panti Adda", "Acting");
ins.setTheInstructorDetail(theInstructorDetail);
theInstructorDetail.setTheInstructor(ins);
session.beginTransaction();
session.save(theInstructorDetail);
session.getTransaction().commit(); 

推荐结构

如果您可以对数据库进行更改,则建议以下其中一项:

选项A):更好

从逻辑上讲,将Instructor用作主表,将InstructorDetail用作辅助表。也就是说,从instructor_detail_id表中删除Instructor列,并在InstructorDetail表中添加instructor_id列。然后,只需翻转java类中的休眠注释配置即可(与上述相反)。

选项B):最佳

由于具有OnetoOne关系,为减少内存的索引占用量,请对两个表使用相同的PK Instructor_Id。然后,您可以使用@MapsId批注,而不必使用Bi-directional association

InstructorDetail.java

 @OneToOne
 @MapsId
 private Instructor theInstructor;