-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JPA / Hibernate, duplicate pkey error when updating entity that is a subclass of a base class that uses IdClass for composite primary key #2767
Comments
Thanks @cuipengfei for the reproducer. Using it I have verified this issue exists. The first thing I noticed between the two scenarios, is that inserting the However, for the When running the
But when running the
It's starting to look like a Hibernate-specific issue. |
I have taken your reproducer and turned things around such that each scenario is a different test case at https://github.com/gregturn/spring-data-jpa-id-class-issues. |
In both situations, it appears to perform a |
Yes. I have noticed the same thing.
I just was not sure if it is a spring-data-jpa issue or a hibernate issue. |
Not arguing with the analysis, but
is problematic, since it mit loose generated information.
This might even be a workaround for the issue. |
I'm going to try and reproduce the test cases using Hibernate only. |
@schauder I actually tested that, and it seemed to make no difference. I created alternative variants of the test cases, instead using an injected I went back and applied |
I've pushed a few updates to my reproducer. This suggests that at a minimum, you can work around this by ensuring your service layer has |
I turned up the logging flags and gathered this from the
|
This is the
|
I seem to also have this issue, or a very similar one. I'm using Spring Boot 2.7.6 and its respective Spring Data JPA/Hibernate versions. I replicated exactly the blogpost by Vlad Mihalcea on using @EmbeddedId and @Embedded to create a compound primary key on a many-to-many table entity, and it gives me the following exception when calling postRepository.save(): Vlad's blogpost with the code is here: https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/ I thought I was going crazy as I double-checked if the code was identical a thousand times and could not figure out why it didn't work. It seems it may be a Spring Data JPA bug then. If I can assist with replicating the bug, I'll gladly help! The workaround I found for now is to remove one side of the many-to-many relationship, so only have it present on Post, for example, but not on Tag. That seems to prevent the exception, but is not ideal in case you would like to use the relation from both entities. |
@ThomHurks When the many-to-many relationship is essential for your issue, it seem to be a different issue, since the original has no such relationship. Please create a separate issue and provide a reproducer. |
@schauder Okay, will do. |
I took a look at the reproducer. Did you notice there are different exceptions thrown? When running without transactions in Hibernate we get These inner transactions trigger the error. Interestingly Hibernate shows the same behaviour with the "Handrolled" transaction, i.e. a transaction implemented using the JPA API. So currently we don't really reproduce the same scenario with Hibernate, since we have no tests with "inner transaction", but since we see the same behaviour with the handrolled transaction I'm still thinking this is a Hibernate issue. I put the test results in a spread sheet: |
Ok, the duplicate key with hand rolled transaction was due to stuff left in the database from previous tests. I created a PR with inner transactions: gregturn/spring-data-jpa-id-class-issues#1 With these the Hibernate behaviour matches that of Spring Data. Therefore closing this issue. |
Hi everyone, I made some tests using the repo with Spring 3.x and the bug does not appear. For someone that needs a composite IdClass and persistence with more than one transaction aspect, mabe is alternative. |
How to reproduce this issue
https://github.com/cuipengfei/Spikes/tree/master/jpa/ClassIdUpdateIssuethis code can reproduce the issue, just run the main method then the error will happen.https://github.com/gregturn/spring-data-jpa-id-class-issues/tree/main/src/test/java/com/example/demo
Just make sure you have docker installed and run these 👆 unit tests.
The unit test with EmbeddedId will be ok.
But the one with IdClass will fail, while we expect it to be successful.
Issue Description
There is a base class like this:
Its IdClass is like this:
Then it has a subclass:
The subclass only adds one additional field, nothing else fancy.
Then when I try to persist an instance of the subclass like this:
The Error
Then there will be an error of duplicate pkey when I try to save the instance of the subclass for the second time after some modification.
The error seems to be related with the @IdClass annotation, because I have tried using @EmbeddedId and @Embeddable for composite pkey, then the error does not happen.
The question
What is the root reason of this issue? Is @IdClass not supposed to be used for this scenario?
links
https://stackoverflow.com/questions/75147518/jpa-hibernate-duplicate-pkey-error-when-updating-entity-that-is-a-subclass-of
https://hibernate.atlassian.net/browse/HHH-16054
The text was updated successfully, but these errors were encountered: