我正在尝试设置我的实体以允许pks。我的数据库由两个字段组成,
dealer_detail_id pk user_detail_id pk
两者都在相应的表中加入id。
到目前为止,我已经尝试过这个但没有成功。
@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;
有人能发现我做错了吗?
答案 0 :(得分:12)
这是正确的:
@Embeddable
public class DealerUserPk implements Serializable {
private Integer dealerDetail;
private Integer userDetail;
需要添加MapsId,如下所示
@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;
尝试一下。
答案 1 :(得分:1)
试试这个
@Embeddable
public class DealerUserPk implements Serializable {
@ManyToOne
private DealerDetail dealerDetail;
@ManyToOne
private UserDetail userDetail;
public void setDealerDetail(DealerDetail dealerDetail) {
this.dealerDetail=dealerDetail;
}
public DealerDetail getDealerDetail(){
return this.dealerDetail;
}
public void setUserDetail(UserDetail userDetail) {
this.userDetail=userDetail;
}
public UserDetail getUserDetail() {
return this.userDetail;
}
}
和
@Entity
@Table(name = "dealer_user")
public class ProductItem {
@Id
private DealerUserPk id= new DealerUserPk();
// --- For bidirectional association---
@SuppressWarnings("unused")
@Column(name="dealer_detail_id", nullable=false, updatable=false, insertable=false)
private Integer dealerDetail;
@SuppressWarnings("unused")
@Column(name="user_details_id", nullable=false, updatable=false, insertable=false)
private Integer userDetail;
// ---
public void setDealerDetail(DealerDetail dealerDetail) {
id.setDealerDetail(dealerDetail);
}
public DealerDetail getDealerDetail(){
return id.getDealerDetail();
}
public void setUserDetail(UserDetail userDetail) {
id.setUserDetail(userDetail);
}
public UserDetail getUserDetail() {
return id.getUserDetail();
}
}