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: Hibernate foreign key as part of primary key and JPA & Hibernate - Composite primary key with foreign key, but no getting idea how to do these two ?