Java 使Hibernate实体中的主键与外键相同
我刚开始冬眠。我在以下两个实体工作: 实体1如下所示:Java 使Hibernate实体中的主键与外键相同,java,mysql,hibernate,jpa,Java,Mysql,Hibernate,Jpa,我刚开始冬眠。我在以下两个实体工作: 实体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
@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 ?
@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
中表示这一点
关于这一点,我已经看了一些帖子:和,但不知道如何做这两个?解决方案是
@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文档中找到更多信息,您需要使用
@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;
}
VM用户类
public class VmUser implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="ID")
private Users user;
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy="user")
private VmUser vmUser;
@Entity
public class A implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="a")
private List<MyEntity> myEntitys;
@Entity
public class B implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="b")
private List<MyEntity> myEntitys;
@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;
@Embeddable
public class MyEntityPK implements Serializable {
@Column(insertable=false, updatable=false)
private long id1;
@Column(insertable=false, updatable=false)
private long id2;
用户类
public class VmUser implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="ID")
private Users user;
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy="user")
private VmUser vmUser;
@Entity
public class A implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="a")
private List<MyEntity> myEntitys;
@Entity
public class B implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="b")
private List<MyEntity> myEntitys;
@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;
@Embeddable
public class MyEntityPK implements Serializable {
@Column(insertable=false, updatable=false)
private long id1;
@Column(insertable=false, updatable=false)
private long id2;
一门课
public class VmUser implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="ID")
private Users user;
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy="user")
private VmUser vmUser;
@Entity
public class A implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="a")
private List<MyEntity> myEntitys;
@Entity
public class B implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="b")
private List<MyEntity> myEntitys;
@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;
@Embeddable
public class MyEntityPK implements Serializable {
@Column(insertable=false, updatable=false)
private long id1;
@Column(insertable=false, updatable=false)
private long id2;
MyEntityPK类
public class VmUser implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="ID")
private Users user;
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@OneToOne(mappedBy="user")
private VmUser vmUser;
@Entity
public class A implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="a")
private List<MyEntity> myEntitys;
@Entity
public class B implements Serializable {
@Id
private long id;
@OneToMany(mappedBy="b")
private List<MyEntity> myEntitys;
@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;
@Embeddable
public class MyEntityPK implements Serializable {
@Column(insertable=false, updatable=false)
private long id1;
@Column(insertable=false, updatable=false)
private long id2;
您将id声明为主键,我认为OP希望将用户_id作为主键,我添加了ColumnMapping@SimonMartinelli请您也为实体2推荐一种方法好吗?我也为第二个实体添加了解决方案。重要的是PK类必须实现Serializable并重写equals和hashCode@SimonMartinelli是 啊你的解决方案中有一个小小的疑问。您不需要添加任何映射,比如属性映射到哪个id吗?我的意思是这里我们有两个Ids注释和两个MapIds注释。那么,它将如何计算出aId对应于A或B,与投标相同?如果我错了,请纠正我。@code mode我没有任何vm\u用户列。所以我想你的答案中应该省略vm_user列。如果我错了,请纠正我。另外,关于实体2,您是否可以建议一种方法?为
vm\u用户添加id
,vm\u用户id
。对于MyEntity
@code模式,您可以执行相同的操作,因为vm\u用户表user\u id是主键。请更新答案。“我希望它能有所帮助。”代码模式,一个小小的疑问。在MapsId注释中,“aId”和“bId”指的是类BKey的属性,对吗?它可能不一定与基础表的实际列名相同。要做到这一点,我必须在BKey类的这些属性之上添加注释列(name=“a_id”)和列(name=“b_id”)。如果我错了,请纠正我。同时显示您的用户实体。。用户主键是int还是string?@GolamMazidSajib,id是与用户hibernate实体关联的用户表的整型主键。