猿问

线程“main” org.hibernate.AnnotationException 中的异常:

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

当我试图在表中保存数据时,我都会收到以下错误。


`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和一对一注释讲师.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 + "]";

}


绝地无双
浏览 143回答 2
2回答

江户川乱折腾

一对一单向协会您的代码设置指向“讲师详细信息”为父级,“教师”为子级关联。为此,您应该在保留/保存教师实体之前保留/保存 InstructorDetail。OneToOne Unidirectional Associationsession.beginTransaction();session.save(theInstructorDetail);session.save(ins);session.getTransaction().commit();一对一双向协会如果您不想在 DB 中关联 FK,请在 java 休眠中创建双向关联:讲师.java@OneToOne @JoinColumn(name = "instructor_detail_id") private InstructorDetail theInstructorDetail;讲师详情.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):更好作为主表和辅助表,在逻辑上更有意义。也就是说,从表中删除列并在InsucateDetail表中添加列。然后,只需翻转java类中的休眠注释配置(与上述相反)。InstructorInstructorDetailinstructor_detail_idInstructorinstructor_id选项 B):最佳由于它是一对一关系,因此若要减少内存上的索引占用空间,请对两个表使用相同的 PK。然后,您可以使用注释而不必使用。Instructor_Id@MapsIdBi-directional association讲师详情.java @OneToOne @MapsId private Instructor theInstructor;

慕桂英4014372

你已经试过了。public Instructor(String firstName, String lastName, String email) {    super();    this.firstName = firstName;    this.lastName = lastName;    this.email = email;    this.theInstructorDetail= new InstructorDetail();}我认为你应该开始所有的属性。
随时随地看视频慕课网APP

相关分类

Java
我要回答