我想在 Spring data jpa 中創建共享主鍵,一切都很好,直到我使用 @ManyToOne 有什么問題?
我的物體:
@Entity
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
private IDCard idCard;
}
@Entity
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class IDCard {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne(cascade = CascadeType.ALL)
private Person person;
private Long inn;
@JsonIgnore
public Person getPerson() {
return person;
}
}
結果(如果我使用@OneToOne,它可以作業):
@OneToOne 結果
當我m switching @OneToMany there的 id 不同時:
@ManyToOne 結果
uj5u.com熱心網友回復:
僅考慮關聯的映射,欄位應類似于:
@Entity
public class Person {
// This field is optional, it's useful only if you want a bidirectional association
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
private List<IDCard> idCards;
}
@Entity
public class IDCard {
@ManyToOne
private Person person;
}
請查看Hibernate ORM 指南中的多對一映射以獲取更多詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483676.html
上一篇:HibernateSearch6中FullTextQuery.setCriteriaQuery()的替代品是什么?
下一篇:在讀取和洗掉行之前休眠鎖定表
