This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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 ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

@ElementCollection/@CollectionTable encountering MySQLIntegrityConstraintViolationException

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!

I would like to kindly ask some help with regards to my weird problem in JPA 2.1 @ElementCollection/@CollectionTable mapping.

I have created the mapping below:



I was able to add, update, and read records from my Database. The issue arises when stop my local web app then start it again, and test the “FindAll” button that executes the select statement for getting the entire Employee in the table.
It hangs and there were logs exceptions that have been thrown, please see below:



I am not sure if I missed something or there are existing bugs in relation to this, but I already upgrade to the latest version of EclipseLink and MySQL Connector for Java.

I am hoping someone already encounter these particular anomalies when trying out these use case in JPA 2.1. I appreciate any help you could impart.

Thanks very much in advance!

Best regards,
Daniel

 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

You got that violation exception because you are trying to insert duplicate key.

Is the EMP_ID column set auto-increment in the database? Since you use GenerationType.IDENTITY for the id, does this value change accordingly (eg different for every Employee)?

If you are only viewing/loading the page, then the query should only have been selects. Hence no violation exception.
 
Daniel Jabonete
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!

I found where the problem lies in my code.

Primarily, it was not on the @ElementCollection or @CollectionTable annotation configurations, but rather on the Department relationship @OneToMany annotation. In which by default has a fetch Type of “javax.persistence.FetchType.LAZY”, so the moment I executed the “SELECT” query statement in my “FindAll” button the Department entity didn’t eagerly load its employees.



I hope this serve as a reference resolution for those who are encountering similar problems with JPA 2.

Thanks very much!

Best regards,
Daniel
 
K. Tsang
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice you found your own problem

I knew that GenerationType thing is fishy not knowing how you set up the database table(s).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!