Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » how to use two @OneToMany and @ManyToOne to replace @ManyToMany
how to use two @OneToMany and @ManyToOne to replace @ManyToMany [message #872725] Wed, 16 May 2012 17:36 Go to next message
Xiwen Luo is currently offline Xiwen Luo
Messages: 2
Registered: April 2012
Junior Member
i am new in jpa and eclipselink
i have post this question on stack overflow ,

but the answer doesn't work for me. Because it suppose to create 3 tables , using @MapsId it create 5 tables.


here is my case :

I am new in eclipselink and trying to add extra columns in manyTomany association table.

so i decide to use two @OneToMany and @ManyToOne to replace @ManyToMany relationship.

i have tried the hibernate way in the test code below, but it does't work.

Is there anyone know how to build this in eclispelink?

Thanks

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:
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
Re: how to use two @OneToMany and @ManyToOne to replace @ManyToMany [message #873255 is a reply to message #872725] Thu, 17 May 2012 20:18 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1034
Registered: July 2009
Senior Member
I saw this on stackoverflow and I am unsure of what you mean by the response requiring 5 tables - I don't believe it does.

The reason why you are getting the error is that JPA does not allow relationships to occur within a primary key class. When you mark an Embeddable as an EmbeddedId, it becomes a primary key class, so the rules still apply.
So it must only use basic types.
But I'm not sure why you need the embeddable at all. The simplest mapping for what you have is (other than using a ManyToMany mapping):

@Entity
public class SideA {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;
  
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "sideA", cascade=CascadeType.ALL)
  private List<ABAssociation> association = new ArrayList<ABAssociation>();
..
}

@Entity
@PKClass(ABAssociationPK.class)
public class ABAssociation {
  @Id
  @ManyToOne
  private SideA sideA;

  @Id
  @ManyToOne
  private SideB sideB;
..
}


public class ABAssociationPK {
  private long sideA;
  private long sideB;
}


If you must use an embeddable - where you are going wrong is your embeddable should look like the ABAssociationPK i've shown above. Its attributes must be of long/Long type instead of a SideA attribute.
You would then use the MapsID instead of @ID in your AB entity:

@Entity
public class ABAssociation {
  @EmbeddedId
  private ABAssociationPK pk = new ABAssociationPK();

  @MapsId
  @ManyToOne
  private SideA sideA;
  @MapsId
  @ManyToOne
  private SideB sideB;
..
}


Please note that in the code you've shown you are putting annotations on both fields and methods which is not allowed, which will cause some to be ignored. They should be either all on properties or all on fields, but not mixed (unless you specify the access type).

Best Regards,
Chris

Previous Topic:Help on clone method
Next Topic:Getting update DDL with SchemaManager
Goto Forum:
  


Current Time: Sat Oct 25 18:23:25 GMT 2014

Powered by FUDForum. Page generated in 0.12310 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software