2014-02-25 111 views
6

我想設置我的實體允許PKS。我的數據庫包含兩個字段,Hibernate的JPA多對一複合鍵

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; 

任何人能發現我在做什麼錯?

回答

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(); 
    } 
    } 
+0

我沒有看到任何生成錯誤,但是當我對錶運行以下查詢時,我得到零結果列表 dealerUsers = session.createCriteria(DealerUser.class).list(); –

+0

我最終只是錯過了@Entity。 –

9

這是正確的:

@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; 
    

與嘗試。

+0

我不能測試你的答案做我很沮喪的設計,只是決定把PK列到位,下降複合PK的事實。事實證明,我錯過了我認爲導致整個問題的@Entity。 –