Repository navigation
Hibernate 7: removing one element of a set keeps the others - #16757
Conversation
Removing one element of a unidirectional hasMany set, with removeFrom* or by binding a smaller set of ids, deleted every join table row of the owner (#16691): "delete from item_role where item_roles_id=?". Hibernate 7 restricts the delete of a row by the key and only the element columns that are not nullable (RestrictedTableMutationBuilder .addKeyRestriction, BasicCollectionPersister.applyDeleteRowRestrictions), and the element column of a GORM join table is nullable. Hibernate 5 restricted by the element. JoinTableCollectionPersister extends BasicCollectionPersister and replaces only its row delete, which restricts by the key and every element column. A row of a collection of entities always has its element. GormPersisterClassResolver gives it to sets and bags of entities in a join table; lists and maps delete by their index, and one-to-many collections keep Hibernate's persister. HibernateMappingContextConfiguration registers the resolver with putIfAbsent on hibernate.persister.resolver, so a resolver the application configures is kept. The schema does not change. JoinTableCollectionPersisterSpec: the persister of each kind of association, an application's resolver is kept, roles a, b, c changed to c, d keep c and d (the case on the issue), and removal from a bag and from a many-to-many still works. With the registration disabled, the set removal and the persister features fail. Docs: the upgrade note. Claude-Session: https://claude.ai/code/session_01MDHn7fC3hsGJ27vFHD2o1p
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. Removing one element of a GORM set no longer deletes the other rows, and lists stay on Hibernate's persister. An annotated soft-delete collection and an identifier bag still take this path.
|
CI run 40140010470 passed (all jobs green). JoinTableCollectionPersisterSpec executed and passed on all matrices. |
…otated collections GormPersisterClassResolver delegates to Hibernate's BasicCollectionPersister when a join-table collection uses an identifier bag, soft-delete annotations, an auxiliary or temporal audit table, custom state management, or custom SQL delete mappings, restricting JoinTableCollectionPersister strictly to unindexed and unannotated sets and bags.
|
Updated: |
jamesfredley
left a comment
There was a problem hiding this comment.
Approving. A soft-deleted collection and an identifier bag stay on Hibernate's persister. Removing one element of a GORM set still keeps the other rows. The two earlier comments are resolved.
✅ All tests passed ✅🏷️ Commit: 75a19d6 Learn more about TestLens at testlens.app/docs. |
Fixes #16691
Based on 8.0.x; independent of the #16701 / #16750 / #16756 stack.
Under Hibernate 7, removing one element of a unidirectional
hasManyset (withremoveFrom*, or by binding a smaller set of ids) deleted every join-table row of the owner:Hibernate 7 builds the row delete from the key and only those element columns that are not nullable (
RestrictedTableMutationBuilder.addKeyRestriction,BasicCollectionPersister.applyDeleteRowRestrictions). The element column of a GORM join table is nullable, and it has to be when several collections share the table with a column each. Hibernate 5 restricted by the element, so it got this right.Fix: a row restriction of our own, as suggested on the issue. No DDL change.
JoinTableCollectionPersisterextendsBasicCollectionPersister. It replaces only the row delete, which restricts by the key and every element column:... where item_roles_id=? and role_id=?. A row of a collection of entities always has its element, so that is safe.GormPersisterClassResolvergives that persister to sets and bags of entities in a join table. Lists and maps already delete by their index, and FK one-to-many collections keep Hibernate's persister.putIfAbsentonhibernate.persister.resolver, so a resolver the application configures is kept.[a, b, c]→[c, d](the issue's comment)[d][c, d]Tests
JoinTableCollectionPersisterSpec(9):[a, b, c]→[c, d]case;grails-data-hibernate7-coresuite passes locally.Merge order with #16701: #16701 marks three shared-join-table removals
@PendingFeaturefor this issue. With both merged they pass, so@PendingFeaturefails them. Whichever of the two merges second drops those three markers. I verified this on the stack locally:SharedJoinTableBulkDeleteSpec10/10.Docs: upgrade note in
upgrading80x.adoc.https://claude.ai/code/session_01MDHn7fC3hsGJ27vFHD2o1p