繁体   English   中英

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

[英]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 aid of b的 id 的组合。 我不知道如何在 Hibernate 实体MyEntity中表示这一点。

关于同样的,我经历了一些帖子: Hibernate foreign key as part of primary keyJPA & 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM