亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

線程“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", "[email protected]");

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 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號