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.
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.
K. Tsang CEng MBCS PMP PMI-ACP OCPJEA OCPJP
posted 5 years ago
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.