Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » OneToMany is not working using composite key in JPA2.0 with Ecliselink(OneToMany is not working using composite key in JPA2.0 with Ecliselink)
OneToMany is not working using composite key in JPA2.0 with Ecliselink [message #933015] Thu, 04 October 2012 12:50 Go to next message
Chandra Narasimha is currently offline Chandra Narasimha
Messages: 5
Registered: July 2012
Junior Member
Hi,

We are using JPA2.0 with Eclipselink, OneToMany relationship is not working when we are using composite key between two entities. Please find the problem in detail

Class Parent

@EmbeddedId
private ParentPK id;

@OneToMany(cascade = CascadeType.ALL, mappedBy="parents")
@JoinColumns(
{ @JoinColumn(name = "A", referencedColumnName = "A"),
@JoinColumn(name = "B", referencedColumnName = "B")})
private Set<Child> childList;

Class ParentPK

@Column(name = "A", nullable=false)
private String aData;

@Column(name = "B", nullable=false)
private String bData;

Note- parent table primary key is (A, B)


Class Child

@EmbeddedId
private ChildPK id;

@ManyToOne(optional = true)
@JoinColumns(
{ @JoinColumn(name = "A", referencedColumnName = "A", insertable = false, updatable = false),
@JoinColumn(name = "B", referencedColumnName = "B", insertable = false, updatable = false)})
private Parent parents;

Class ChildPK

@Column(name = "A", nullable=false)
private String aData;

@Column(name = "B", nullable=false)
private String bData;

@Column(name = "C", nullable=false)
private String cData;

Note- child table primary key is (A, B, C) and this has refernece to parent table with A, B foreighn key relationship.

I am trying simple entityManager find with Parent class and PArentPK

Parent parent = entityManager.find(Parent.class, parentPK);
System.out.println(" Child list ===> "+ parent.getChildList());

Child list is empty, we are not able to retrieve child list.

I tried all the combinations it is not working, Can some one please suggest us what could be the cause?

Thanks
Chandra
Re: OneToMany is not working using composite key in JPA2.0 with Ecliselink [message #939159 is a reply to message #933015] Wed, 10 October 2012 09:30 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Enable logging, is the generated SQL correct? Is there any data in your database?

How did the data get into your database? If you inserted through JPA, ensure it was inserted correctly. When you add a child to your parent, you must ensure you set the child's id to contain the parent's id values, and you must ensure that you set the ManyToOne back, and add the child to the parent's OneToMany collection.

In general it is normally simpler to not use an EmbeddedId and instead use an IdClass, and put the @Id annotation on the ManyToOne.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0


James : Wiki : Book : Blog : Twitter
Previous Topic:Query on a attribute in the interface that is used for variable 1-1 mapping
Next Topic:Implemeting postMerge events
Goto Forum:
  


Current Time: Thu Jul 24 04:30:24 EDT 2014

Powered by FUDForum. Page generated in 0.01691 seconds