File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Could not generate OneToMany records using @ElementCollection in JPA 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Could not generate OneToMany records using @ElementCollection in JPA 2" Watch "Could not generate OneToMany records using @ElementCollection in JPA 2" New topic

Could not generate OneToMany records using @ElementCollection in JPA 2

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Dear JPA Specialists,

I need your advice on getting a OneToMany (Employee to Photo) relational mapping using @ElementCollection & @CollectionTable in JPA 2.0. Below is a hypothetical code snippet that I am trying to get all the Photo records persisted without success:

However, only the last photo (afterWorkPhoto) object has been persisted to the Employee entity instead of all 3 photos. I also want the option of going back to add more new (unique) photos for the same employee in the future.

I am running Eclipselink 2.4.1 (JPA 2.0), Java 7 on Windows XP & 7.

Your advice would be much appreciated.

Thanks in advance,

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
In your code you call createEmployee with an Employee but your code takes String, String, ..., so what you posted is not the real code.
Your createEmployee seems to be creating a new Employee, so make sure you are adding the photos to that employee.

Also, your addPhotos method first does a contains, ensure that your equals and hashCode methods are correct.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
I agree. Here's the link:
subject: Could not generate OneToMany records using @ElementCollection in JPA 2
Similar Threads
persistence.xml file for MS SQL Server 2000
Value is session managed bean's attribute is nulled
Problem with refresh() method of EntityManamger in JPA
JPA - How to persist new table without exception (SE) environment
Can not run JPA sample example using J2SE