0

我是 eclipselink 的新手,并试图在 manyTomany 关联表中添加额外的列。

所以我决定使用两个@OneToMany 和@ManyToOne 来替换@ManyToMany 关系。

我在以下链接中尝试了休眠方式,但它不起作用。 http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

有谁知道如何在 eclispelink 中构建它?

谢谢

--------------只需点击上面的链接并粘贴测试代码 --------------

A面:

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

}

边B:

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

}

联想:

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

ABA协会PK:

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

4

1 回答 1

3

信息很清楚:

与嵌入式 ID 规范一起使用的可嵌入类只能包含基本映射。

因此,ABAssociationPK 类中不能有 ManyToOne 关联。

执行此操作的标准 JPA 方法是使用@MapsId注释。javadoc 提供了一个有用的示例。在您的特定情况下,ABAssociationPK该类应包含两个类型的字段long:aIdbId,映射为基本列。

Association实体应包含关联,并使用注释@MapsId

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

@ManyToOne
@MapsId("bId")
public SideB getSideB() {
    return sideB;
}
于 2012-05-16T08:15:58.200 回答