Hibernate JPA ManyToOne复合键

时间:2014-02-25 01:20:25

标签: java hibernate jpa

我正在尝试设置我的实体以允许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;

有人能发现我做错了吗?

2 个答案:

答案 0 :(得分:12)

这是正确的:

@Embeddable
public class DealerUserPk implements Serializable {

    private Integer dealerDetail;
    private Integer userDetail;
  1. 但您的DealerUser注释为嵌入式应该是@Entity 因为你正在使用@Table注释。
  2. 需要添加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;
    
  3. 尝试一下。

答案 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();
    }
  }