英文:
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; <--- 如何表示VmUser实体的主键?
在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
和b的id
的组合。我不知道如何在Hibernate实体MyEntity
中表示这一点。
关于这个问题,我阅读了一些帖子:https://stackoverflow.com/questions/7146671/hibernate-foreign-key-as-part-of-primary-key 和 https://stackoverflow.com/questions/58527109/jpa-hibernate-composite-primary-key-with-foreign-key,但是我还不了解如何实现这两个要求。
英文:
I am new to Hibernate. I am working on two entities as follows:
Entity 1 is as follows:
@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 ?
In the associated table in mysql i.e. vm_user
, user_id
is both primary key as well as foreign key which refers to id of user
table associated with User
entity.
Entity 2 is as follows:
@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;
In the associated table in mysql i.e. my_entity
, primary key is a combination of id of a
and id of b
. I am not getting how to denote this in Hibernate entity MyEntity
.
Regarding the same, I have gone through a few posts: https://stackoverflow.com/questions/7146671/hibernate-foreign-key-as-part-of-primary-key and https://stackoverflow.com/questions/58527109/jpa-hibernate-composite-primary-key-with-foreign-key, but no getting idea how to do these two ?
答案1
得分: 3
解决方案是 @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;
// 重要:重写 equals() 和 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_single/Hibernate_User_Guide.html#identifiers-derived
英文:
The solution is @MapsId
For example
@Entity
@Table(name = "vm_user")
public class VmUser implements Serializable {
@Id
@Column(name = "user_id")
private Integer id;
@MapsId
@OneToOne
private User user;
You can also remove the @JoinColumn(unique = true)
because @Id
makes it unique already.
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;
Please find more information in the Hibernate documentation https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#identifiers-derived
答案2
得分: 2
你需要使用 @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;
}
英文:
You need to use @EmbeddedId
and @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;
}
You can do the same thing for MyEntity as below,
@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;
}
答案3
得分: 0
**VM Users 类**
public class VmUser implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="ID")
private Users user;
**Users 类**
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy="user")
private VmUser vmUser;
**A 类**
@Entity
public class A implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="a")
private List<MyEntity> myEntitys;
**B 类**
@Entity
public class B implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="b")
private List<MyEntity> myEntitys;
**MyEntity 类**
@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 类**
@Embeddable
public class MyEntityPK implements Serializable {
@Column(insertable=false, updatable=false)
private long id1;
@Column(insertable=false, updatable=false)
private long id2;
英文:
VM Users class
public class VmUser implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="ID")
private Users user;
Users class
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy="user")
private VmUser vmUser;
A 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;
MyEntity 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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论