使Hibernate实体中的主键与外键相同

huangapple go评论80阅读模式
英文:

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的idb的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 = &quot;vm_user&quot;)
public class VmUser implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = &quot;created_by&quot;)
    private String createdBy;

    @Column(name = &quot;last_modified_by&quot;)
    private String lastModifiedBy;

    @Column(name = &quot;created_date&quot;)
    private Instant createdDate;

    @Column(name = &quot;last_modified_date&quot;)
    private Instant lastModifiedDate;

    @OneToOne
    @JoinColumn(unique = true)
    private User user;             &lt;--- 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 = &quot;my_entity&quot;)
public class MyEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = &quot;created_by&quot;)
    private String createdBy;

    @Column(name = &quot;last_modified_by&quot;)
    private String lastModifiedBy;

    @Column(name = &quot;created_date&quot;)
    private Instant createdDate;

    @Column(name = &quot;last_modified_date&quot;)
    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 = &quot;vm_user&quot;)
public class VmUser implements Serializable {

    @Id
    @Column(name = &quot;user_id&quot;)    
    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 = &quot;my_entity&quot;)
public class MyEntity implements Serializable {

    @Id
    private Integer aId;
    @Id
    private Integer bId;

    @MapsId(&quot;aId&quot;)
    @ManyToOne
    private A a;

    @MapsId(&quot;bId&quot;)
    @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 = &quot;vm_user&quot;)
public class VmUser implements Serializable {
   @Id
    @Column(name = &quot;user_id&quot;)    
    private Integer id;

    @MapsId(&quot;user_id&quot;)
    @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 = &quot;my_entity&quot;)
public class MyEntity implements Serializable {

    @EmbeddedId
    private BKey primaryKey;
    
    @MapsId(&quot;aId&quot;)
    @ManyToOne
    private A a;

    @MapsId(&quot;bId&quot;)
    @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=&quot;ID&quot;)
private Users user;

Users class

public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy=&quot;user&quot;)
private VmUser vmUser;

A class

@Entity
public class A implements Serializable {
@Id
private long id;
@OneToMany(mappedBy=&quot;a&quot;)
private List&lt;MyEntity&gt; myEntitys;

B Class

@Entity
public class B implements Serializable {
@Id
private long id;
@OneToMany(mappedBy=&quot;b&quot;)
private List&lt;MyEntity&gt; myEntitys;

MyEntity class

@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private MyEntityPK id;
@ManyToOne
@JoinColumn(name=&quot;ID1&quot;)
private A a;
@ManyToOne
@JoinColumn(name=&quot;ID2&quot;)
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;

huangapple
  • 本文由 发表于 2020年5月2日 14:37:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61555425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定