Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@ElementCollection/@CollectionTable encountering MySQLIntegrityConstraintViolationException

 
Daniel Jabonete
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

 
K. Tsang
Bartender
Posts: 3585
16
Android 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: 3585
16
Android 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic