Lukas Witzani (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMTJkNTk3ZTZk...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16037?atlOrigin=eyJpIjoiMTJkNT...
) HHH-16037 (
https://hibernate.atlassian.net/browse/HHH-16037?atlOrigin=eyJpIjoiMTJkNT...
) merge of a persisted entity uses an insert statement instead of an update statement
resulting in duplicate entities (
https://hibernate.atlassian.net/browse/HHH-16037?atlOrigin=eyJpIjoiMTJkNT...
)
Change By: Lukas Witzani (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
h1. Summary
* after updating spring boot to 3.0.1 which also updates hibernate to 6.1.6.Final it does
not seem to be possible to update an entity with this constellation
* the main difference is that an additional and unexpected where clause is added which
prevents finding the correct entity in this case
* the result is that a new entity is persisted instead of the existing one being updated
h2. Minimal project for reproduction
*
[
https://github.com/lwitzani/hibernate6_singleTableIssue|https://github.co...]
* project has two branches (one with hibernate 5 and one with hibernate 6)
* the only difference in the branches is the updated versions of spring boot and therefore
hibernate
* i debugged very deep and think that it is a problem with hibernate at the location where
the select statement is generated ({{LoaderSelectBuilder}}.{{createSelect}})
h2. Requisites
* entity ("Person") exists that has a field which is a set of other entities
("Set<Leg>")
* these other entities are derived from an abstract super class ("Leg extends
BodyPart")
* the abstract super class' inheritance strategy is SINGLE_TABLE
!uml.png|width=517,height=425!
h2. Reproduction
* execute the test PersonPersistenceTest.shouldUseSQLUpdateToChangeThePersonName
* the test basically
** creates a Person (without Legs) and saves it using the repository
** refetches the Person by findById
** changes the Person's name
** saves the changed Person again
h2. Observations in Hibernate 5.6.14.Final
* when the second save is called (after changing the name) there will be a select
statement which is used to check if the entity already exists
** the body_part table is joined via left outer join
** the where clause here only consists of "where [person.id|http://person.id] =
<id>"
* the entity is correctly found by the select statement so afterwards an update statement
follows
* result: one entity exists (the changed one)
!hibernate5.png|width=1265,height=692!
h2. Observations in Hibernate 6.1.6.Final
* when the second save is called (after changing the name) there will be a different
select statement now
** the body_part table is joined via left join now (not outer)
** the critical change here is that the where clause now suddenly consists of "where
body_part.discriminator = 'LegBodyPart' and [person.id|http://person.id] =
<id>"
* although the entity exists, it is not found by the select statement so afterwards an
insert statement follows which creates a second Person with a new id
* result: two entities exist (the original one and the changed one)
!hibernate6.png|width=1179,height=644!
h1. Summary
* after updating spring boot This issue might be heavily related to 3 [
https://hibernate.
0 atlassian. 1 which also updates net/browse/HHH-16053|https:// hibernate to 6. 1
atlassian. 6.Final it does not seem to be possible to update an entity with this
constellation net/browse/HHH-16053|smart-link]
* the main difference is that an additional and unexpected where clause is added which
prevents finding the correct entity in this case maybe related to:
* the result is that a new entity is persisted instead of the existing one being updated
[
https://hibernate.atlassian.net/browse/HHH-15876|https://hibernate.atlass...]
*
[
https://hibernate.atlassian.net/browse/HHH-15829|https://hibernate.atlass...]
(
https://hibernate.atlassian.net/browse/HHH-16037#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16037#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100214- sha1:771690a )