Skip to main content


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 16:50 Go to next message
Chandra Narasimha is currently offline Chandra NarasimhaFriend
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 13:30 Go to previous message
James Sutherland is currently offline James SutherlandFriend
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: Fri Apr 26 00:01:26 GMT 2024

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

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

Back to the top