[英]Hibernate Entity OneToOne with same Primary and Foreign Key Bidirectional Relationship
[英]Making primary key same as foreign key in Hibernate entity
我是 Hibernate 的新手。 我正在研究兩個實體,如下所示:
實體 1 如下:
@Entity
@Table(name = "vm_user")
public class VmUser implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "created_by")
private String createdBy;
@Column(name = "last_modified_by")
private String lastModifiedBy;
@Column(name = "created_date")
private Instant createdDate;
@Column(name = "last_modified_date")
private Instant lastModifiedDate;
@OneToOne
@JoinColumn(unique = true)
private User user; <--- HOW WILL I DENOTE THIS PRIMARY KEY OF VMUSER ENTITY ?
在 mysql 的關聯表中,即vm_user
中, user_id
既是主鍵,也是外鍵,指的是與User
實體關聯的user
表的 id。
實體 2 如下:
@Entity
@Table(name = "my_entity")
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "created_by")
private String createdBy;
@Column(name = "last_modified_by")
private String lastModifiedBy;
@Column(name = "created_date")
private Instant createdDate;
@Column(name = "last_modified_date")
private Instant lastModifiedDate;
@ManyToOne
private A a;
@OneToOne
@JoinColumn(unique = true)
private B b;
在 mysql 即my_entity
的關聯表中,主鍵是 a 的id of a
和id of b
的 id 的組合。 我不知道如何在 Hibernate 實體MyEntity
中表示這一點。
關於同樣的,我經歷了一些帖子: Hibernate foreign key as part of primary key和JPA & Hibernate - Composite primary key with foreign key ,但不知道如何做這兩個?
解決方案是@MapsId
例如
@Entity
@Table(name = "vm_user")
public class VmUser implements Serializable {
@Id
@Column(name = "user_id")
private Integer id;
@MapsId
@OneToOne
private User user;
您還可以刪除@JoinColumn(unique = true)
,因為@Id
已經使其獨一無二。
public class MyEntityPk implements Serializable {
private Integer aId;
private Integer bId;
// IMPORTANT: Override equals() and hashCode()
}
@IdClass(MyEntityPk.class)
@Entity
@Table(name = "my_entity")
public class MyEntity implements Serializable {
@Id
private Integer aId;
@Id
private Integer bId;
@MapsId("aId")
@ManyToOne
private A a;
@MapsId("bId")
@OneToOne
private B b;
請在 Hibernate 文檔中找到更多信息https://docs.jboss.org_hibernate/orm/5.4/userguide/html_shtmlidentifier-derived_Guide
您需要使用@EmbeddedId
和@MapsId
,
@Entity
@Table(name = "vm_user")
public class VmUser implements Serializable {
@Id
@Column(name = "user_id")
private Integer id;
@MapsId("user_id")
@OneToOne
private User user;
}
您可以對 MyEntity 執行相同的操作,如下所示,
@Embeddable
class BKey {
private int aId;
private int bId;
}
@Entity
@Table(name = "my_entity")
public class MyEntity implements Serializable {
@EmbeddedId
private BKey primaryKey;
@MapsId("aId")
@ManyToOne
private A a;
@MapsId("bId")
@OneToOne
@JoinColumn(unique = true)
private B b;
}
虛擬機用戶 class
public class VmUser implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="ID")
private Users user;
用戶 class
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy="user")
private VmUser vmUser;
一個class
@Entity
public class A implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="a")
private List<MyEntity> myEntitys;
B Class
@Entity
public class B implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="b")
private List<MyEntity> myEntitys;
我的實體 class
@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private MyEntityPK id;
@ManyToOne
@JoinColumn(name="ID1")
private A a;
@ManyToOne
@JoinColumn(name="ID2")
private B b;
MyEntityPK class
@Embeddable
public class MyEntityPK implements Serializable {
@Column(insertable=false, updatable=false)
private long id1;
@Column(insertable=false, updatable=false)
private long id2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.