I'm trying to setup my entity to allow to pks. My database consist of two fields,
dealer_detail_id pk
user_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 @Entity
as 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.
Try this
@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;
}
}
and
@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();
}
}