本文介绍了Hibernate JPA ManyToOne组合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图设置我的实体以允许pks。我的数据库由两个字段组成:
dealer_detail_id pk
user_detail_id pk
相应的表格。
迄今为止我尝试过这种方法,但没有成功。
@Embeddable
public class DealerUserPk实现Serializable {
$ b $ private私人整数dealerDetail;
private Integer userDetail;
DealerUser
@Embeddable
@Table(name =dealer_user,schema =account)
public class DealerUser实现Serializable {
@EmbeddedId
private DealerUserPk id;
@Id
@ManyToOne
@JoinColumn(name =dealer_detail_id,referencedColumnName =id)
私人DealerDetail dealerDetail;
@Id
@ManyToOne
@JoinColumn(name =user_detail_id,referencedColumnName =id)
private UserDetail userDetail;
DealerDetail
@Entity
@Table(name =dealer_detail,schema =account)
public class DealerDetail实现Serializable {
@Id
private整数ID;
UserDetail
@Entity
@Table(name =user_detail,schema =account)
public class UserDetail实现Serializable {
@Id
private整数ID;
任何人都可以发现我做错了什么?
@Embeddable
public class =h2_lin>解决方案
DealerUserPk实现Serializable {
私有整数dealerDetail;
private Integer userDetail;
- 但是您的DealerUser被注释为可嵌入,它应该是@Entity
,因为您使用的是@Table注解。
-
需要添加MapsId,如下所示
@Entity
@Table(name =dealer_user,schema =account)
public class DealerUser实现Serializable {
@EmbeddedId
私人DealerUserPk ID;
@MapsId(dealerDetail)
@ManyToOne
@JoinColumn(name =dealer_detail_id,referencedColumnName =id)
私人DealerDetail dealerDetail;
@Id
@MapsId(userDetail)
@JoinColumn(name =user_detail_id,referencedColumnName =id)
private UserDetail userDetail;
试试看。
I'm trying to setup my entity to allow to pks. My database consist of two fields,
dealer_detail_id pkuser_detail_id pk
Both join on id in corresponding tables.
I've tried this thus far without success.
@Embeddable
public class DealerUserPk implements Serializable {
private Integer dealerDetail;
private Integer userDetail;
DealerUser
@Embeddable
@Table(name = "dealer_user", schema = "account")
public class DealerUser implements Serializable {
@EmbeddedId
private DealerUserPk id;
@Id
@ManyToOne
@JoinColumn(name = "dealer_detail_id", referencedColumnName = "id")
private DealerDetail dealerDetail;
@Id
@ManyToOne
@JoinColumn(name = "user_detail_id", referencedColumnName = "id")
private UserDetail userDetail;
DealerDetail
@Entity
@Table(name = "dealer_detail", schema = "account")
public class DealerDetail implements Serializable {
@Id
private Integer id;
UserDetail
@Entity
@Table(name = "user_detail", schema = "account")
public class UserDetail implements Serializable {
@Id
private Integer id;
Can anybody spot what I'm doing wrong?
解决方案
This is correct:
@Embeddable
public class DealerUserPk implements Serializable {
private Integer dealerDetail;
private Integer userDetail;
- But your DealerUser is annotated with embeddable it should be @Entityas you are using @Table annotation.
Need to add MapsId as it follows
@Entity @Table(name = "dealer_user", schema = "account") public class DealerUser implements Serializable { @EmbeddedId private DealerUserPk id; @MapsId("dealerDetail") @ManyToOne @JoinColumn(name = "dealer_detail_id", referencedColumnName = "id") private DealerDetail dealerDetail; @Id @MapsId("userDetail") @JoinColumn(name = "user_detail_id", referencedColumnName = "id") private UserDetail userDetail;
Try with that.
这篇关于Hibernate JPA ManyToOne组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!