Lukas Witzani (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYWI2ZjM0MTVj...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16037?atlOrigin=eyJpIjoiYWI2Zj...
) HHH-16037 (
https://hibernate.atlassian.net/browse/HHH-16037?atlOrigin=eyJpIjoiYWI2Zj...
) 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=eyJpIjoiYWI2Zj...
)
Issue Type: Bug Affects Versions: 6.1.6 Assignee: Unassigned Attachments: hibernate5.png,
hibernate6.png, uml.png Components: hibernate-core Created: 13/Jan/2023 01:32 AM Priority:
Major Reporter: Lukas Witzani (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
--------------------------------
Minimal project for reproduction
--------------------------------
*
https://github.com/lwitzani/hibernate6_singleTableIssue (
https://github.com/lwitzani/hibernate6_singleTableIssue )
* 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 )
----------
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
------------
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
--------------------------------------
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)
(
https://hibernate.atlassian.net/secure/attachment/50159/50159_hibernate5.png )
-------------------------------------
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)
(
https://hibernate.atlassian.net/secure/attachment/50160/50160_hibernate6.png )
*******
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
(
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:b03f6a4 )