为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

绝地无双 2022-08-17 16:53:20
嗨,我刚刚创建了一个应用程序,该应用程序通过一对一映射保存表中的数据当我试图在表中保存数据时,我都会收到以下错误。`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;}@Overridepublic String toString() {    return "Instructor [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email            + ", theInstructorDetail=" + theInstructorDetail + "]";}
查看完整描述

2 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

一对一单向协会

您的代码设置指向“讲师详细信息”为父级,“教师”为子级关联。为此,您应该在保留/保存教师实体之前保留/保存 InstructorDetail。OneToOne Unidirectional Association


session.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;


查看完整回答
反对 回复 2022-08-17
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

你已经试过了。


public Instructor(String firstName, String lastName, String email) {

    super();

    this.firstName = firstName;

    this.lastName = lastName;

    this.email = email;

    this.theInstructorDetail= new InstructorDetail();

}

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


查看完整回答
反对 回复 2022-08-17
  • 2 回答
  • 0 关注
  • 141 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号