2009-08-03 53 views
0

是否可以在使用3個PK列的連接中使用@ManyToMany映射?使用3個ID字段在連接/鏈接表中映射ManyToMany

我相當肯定這是不可能的,解決方法是使用@OneToMany公開實體,這是我過去所做的,但因爲這個領域只是一個PK,我認爲可能有一些聰明的映射來救我這項工作。

問候充滿希望 - JamesC

更新響應ChssPly

這裏是我的 '加入' 實體的一個片段。

@SuppressWarnings("serial") 
    @Embeddable 
    public static class Id implements Serializable { 

     @Column(name = StringPool.Column.APPLICATION_ID) 
     private Long applicationId; 

     @Column(name = StringPool.Column.ARTICLECATEGORY_ID) 
     private Long articleCategoryId; 

     @Column(name = StringPool.Column.USER_ID) 
     private Long userId; 

     public Id() {} 

     public Id(Long applicationId, Long articleCategoryId, Long userId) { 
      this.applicationId = applicationId; 
      this.articleCategoryId = articleCategoryId; 
      this.userId = userId; 
     } 

     public boolean equals(Object o) { 
      if (o instanceof Id) { 
       Id that = (Id)o; 
       return this.applicationId.equals(that.applicationId) && 
         this.articleCategoryId.equals(that.articleCategoryId) && 
         this.userId.equals(that.userId); 
      } else { 
       return false; 
      } 
     } 

     public int hashCode() { 
      return applicationId.hashCode() + articleCategoryId.hashCode() + userId.hashCode(); 
     } 
    } 

回答

3

這取決於你的意思是「3 PK欄」。你正在鏈接的一個實體是否有複合鍵?你當然可以指定多個連接列:

@Entity 
public class MyEntity { 
@ManyToMany(targetEntity=MyOtherEntity.class) 
@JoinTable(name="MY_JOIN_TABLE", 
    [email protected](name="ENTITY_ID"), 
    inverseJoinColumns={@JoinColumn(name="OTHER_ID1"), @JoinColumn(name="OTHER_ID2")}) 
    public Collection getOthers() { 
     return employees; 
    } 
} 

如果這不是你的意思,請澄清你的問題。

+0

會提示基本上相同的東西 – 2009-08-03 21:18:28