I have created a sample project to isolate an issue from a bigger project.
In this project we have entities with composite keys: looks like the problem is related to using @IdClass with derived identities.
Here is the repo with the failing test case (confirmed by Travis CI) :
https://github.com/brunomendola/spring-jpa-composite-pk-with-relationships
The failing test class: https://github.com/brunomendola/spring-jpa-composite-pk-with-relationships/blob/master/src/test/java/net/brunomendola/demo/spring/jpa/compositepkwithrelationships/SpringJpaCompositePkWithRelationshipsApplicationTests.java
After saving 3 objects (which are correctly written to the database), the subsequent select for the same rows mixes up the JoinColumns and, forcing a flush, throws the following exception:
{noformat} org.springframework.orm.jpa.JpaSystemException: identifier of an instance of net.brunomendola.demo.spring.jpa.compositepkwithrelationships.model.ProfileAction was altered from ProfileActionPK(profile=PR1, action=ActivityActionPK(activity=AT1, subId=AT1)) to ProfileActionPK(profile=PR1, action=ActivityActionPK(activity=AT1, subId=1)); nested exception is org.hibernate.HibernateException: identifier of an instance of net.brunomendola.demo.spring.jpa.compositepkwithrelationships.model.ProfileAction was altered from ProfileActionPK(profile=PR1, action=ActivityActionPK(activity=AT1, subId=AT1)) to ProfileActionPK(profile=PR1, action=ActivityActionPK(activity=AT1, subId=1)) at net.brunomendola.demo.spring.jpa.compositepkwithrelationships.SpringJpaCompositePkWithRelationshipsApplicationTests.testCreateProfileActions(SpringJpaCompositePkWithRelationshipsApplicationTests.java:56) Caused by: org.hibernate.HibernateException: identifier of an instance of net.brunomendola.demo.spring.jpa.compositepkwithrelationships.model.ProfileAction was altered from ProfileActionPK(profile=PR1, action=ActivityActionPK(activity=AT1, subId=AT1)) to ProfileActionPK(profile=PR1, action=ActivityActionPK(activity=AT1, subId=1)) at net.brunomendola.demo.spring.jpa.compositepkwithrelationships.SpringJpaCompositePkWithRelationshipsApplicationTests.testCreateProfileActions(SpringJpaCompositePkWithRelationshipsApplicationTests.java:56) {noformat}
Since the cause is a HibernateException, I'm supposing it's a hibernate-core issue; correct me if I'm wrong.
In the repo there are 2 more branches in which the test passes successfully, switching to @EmbeddedId or @IdClass with simple types. But I know it should work also with @IdClass and derived identities, since there are [related tests in spring-data-jpa repository|https://github.com/spring-projects/spring-data-jpa/blob/master/src/test/java/org/springframework/data/jpa/repository/RepositoryWithIdClassKeyTests.java].
I'm trying to map an existing database, so I don't have any freedom regarding the schema.
Let me know if you need more info. Thank you! |
|