2012-05-16 58 views
0

我是eclipselink中的新手,並試圖在manyTomany關聯表中添加額外的列。EclipseLink如何使用兩個@OneToMany和@ManyToOne替換@ManyToMany

所以我決定使用兩個@OneToMany和@ManyToOne替換@ManyToMany關係。

我已經在下面的鏈接嘗試過休眠的方式,但它沒有工作。 http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

有沒有人知道如何在eclispelink中構建它?

謝謝

-------------------------只需按照上面的鏈接粘貼測試代碼------- -------

SIDEA:

@Entity 
@Table(name = "SideA") 
    public class SideA { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    private List<ABAssociation> association = new ArrayList<ABAssociation>(); 


    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.sideA", cascade=CascadeType.ALL) 
    public List<ABAssociation> getAssociation() { 
      return this.association; 
    } 

    public void setAssociation(List<ABAssociation> association) { 
    this.association = association; 
    } 

} 

SideB:

@Entity 
@Table(name = "SideB") 
public class SideB { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 


    private List<ABAssociation> association = new ArrayList<ABAssociation>(); 


    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.sideB", cascade=CascadeType.ALL) 
    public List<ABAssociation> getAssociation() { 
     return this.association; 
    } 

    public void setAssociation(List<ABAssociation> association) { 
     this.association = association; 
    } 

} 

Associtation:

@Entity 
@Table(name = "ABAssociation") 
@AssociationOverrides({ 
    @AssociationOverride(name = "pk.sideA", 
    joinColumns = @JoinColumn(name = "SIDEA_ID")), 
    @AssociationOverride(name = "pk.sideB", 
    joinColumns = @JoinColumn(name = "SIDEB_ID")) }) 
public class ABAssociation { 
    private ABAssociationPK pk = new ABAssociationPK(); 
    @EmbeddedId 
    public ABAssociationPK getPk() { 
     return pk; 
    } 
    public void setPk(ABAssociationPK pk) { 
     this.pk = pk; 
    } 
    @Transient 
    public SideA getSideA() { 
     return getPk().getSideA(); 
    } 
    public void setSideA(SideA sideA) { 
     getPk().setSideA(sideA); 
    } 
    @Transient 
    public SideB getSideB() { 
     return getPk().getSideB(); 
    } 
    public void setSideB(SideB sideB) { 
     getPk().setSideB(sideB); 
    } 
    private String extracolumn; 
} 

ABAssociationPK:

@Embeddable 
public class ABAssociationPK implements java.io.Serializable{ 
    private static final long serialVersionUID = -3797694126054440157L; 

    private SideA sideA; 
    private SideB sideB; 
    public ABAssociationPK(){} 

    @ManyToOne 
    public SideA getSideA() { 
     return sideA; 
    } 

    public void setSideA(SideA sideA) { 
     this.sideA = sideA; 
    } 

    @ManyToOne 
    public SideB getSideB() { 
     return sideB; 
    } 

    public void setSideB(SideB sideB) { 
     this.sideB = sideB; 
    } 

} 

的例外是:Exception Description: Predeployment of PersistenceUnit [testPU] failed. Internal Exception: Exception [EclipseLink-7298] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException Exception Description: The mapping [sideA] from the embedded ID class [class com.fuhu.nabisync.resource.model.entity.ABAssociationPK] is an invalid mapping for this class. An embeddable class that is used with an embedded ID specification (attribute [pk] from the source [class com.fuhu.nabisync.resource.model.entity.ABAssociation]) can only contain basic mappings. Either remove the non basic mapping or change the embedded ID specification on the source to be embedded.

+0

什麼不起作用?碼?例外? –

+0

它的休眠風格......它不能應用於eclipselink:例如「@OneToMany(fetch = FetchType.LAZY,mappedBy =」pk.category「)」pk是關聯表的@embededid。但這不適用於eclipselink。 – Xiwen

+0

什麼不行?碼?例外?爲什麼首先在「加入」實體中使用嵌入式ID? –

回答

3

的信息是很清楚:

,用於與嵌入式ID規範可以 僅含有可嵌入類基本映射。

所以,你不能在ABAssociationPK類中有一個ManyToOne關聯。

執行此操作的標準JPA方法是使用@MapsId annotation。 javadoc提供了一個有用的例子。在您的特定情況下,ABAssociationPK類應包含兩個類型爲longaIdbId的字段,映射爲基本列。

Association實體應包含的關聯,並使用@MapsId註釋:

@ManyToOne 
@MapsId("aId") 
public SideA getSideA() { 
    return sideA; 
} 

@ManyToOne 
@MapsId("bId") 
public SideB getSideB() { 
    return sideB; 
} 
+0

謝謝你的回答,但我用你的指令改變測試代碼,eclipselink生成5個表格:ABASSOCIATION,SIDEA,SIDEA_ABASSOCIATION,SIDEB,SIDEB _ABASSOCIATION。這是正常的嗎?我如何刪除SIDEA_ABASSOCIATION和SIDEB _ABASSOCIATION。謝謝 – Xiwen

+0

我還沒有指定ManyToOne關聯的映射。只要MapsId註釋在那裏,就可以隨意映射它們。 –

+0

您正在獲取SIDEA_ABASSOCIATION和SIDEB _ABASSOCIATION,因爲SideA-> AbAssocations映射必須缺少mappedby規範。沒有那個或其他映射規範,OneToMany將默認使用連接表,就像ManyToMany一樣,這些是默認表名。 – Chris

相關問題