Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Could not generate OneToMany records using @ElementCollection in JPA 2

Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic