Skip to content

Hibernate 7: removing one element of a set keeps the others - #16757

Merged
borinquenkid merged 2 commits into
8.0.xfrom
fix/h7-set-row-delete-8.0.x
Oct 25, 2026
Merged

borinquenkid merged 2 commits into
8.0.xfrom
fix/h7-set-row-delete-8.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

Fixes #16691

Based on 8.0.x; independent of the #16701 / #16750 / #16756 stack.

Under Hibernate 7, 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:

delete from item_role where item_roles_id=?

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.

  • JoinTableCollectionPersister extends BasicCollectionPersister. 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.
  • GormPersisterClassResolver gives 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.
  • It's registered with putIfAbsent on hibernate.persister.resolver, so a resolver the application configures is kept.
Case Before After
set on its own join table, remove 1 of 2 both rows deleted other row stays
roles [a, b, c] → [c, d] (the issue's comment) [d] [c, d]
bag, bidirectional many-to-many, list unchanged unchanged

Tests

  • New JoinTableCollectionPersisterSpec (9):
    • which persister each kind of association gets;
    • a resolver the application configures is kept;
    • the issue's [a, b, c] → [c, d] case;
    • bag and many-to-many removal still work.
  • Mutation-checked: with the registration disabled, the set removal and persister features fail.
  • Full grails-data-hibernate7-core suite passes locally.

Merge order with #16701: #16701 marks three shared-join-table removals @PendingFeature for this issue. With both merged they pass, so @PendingFeature fails them. Whichever of the two merges second drops those three markers. I verified this on the stack locally: SharedJoinTableBulkDeleteSpec 10/10.

Docs: upgrade note in upgrading80x.adoc.

https://claude.ai/code/session_01MDHn7fC3hsGJ27vFHD2o1p

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

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.
@borinquenkid

Copy link
Copy Markdown
Member Author

Updated: GormPersisterClassResolver now explicitly guards against identifier collections (!metadata.isIdentified()), soft-delete collections (metadata.getSoftDeleteColumn() == null), auxiliary/temporal tables (metadata.getAuxiliaryTable() == null), custom state management (metadata.getStateManagementType() == null), and custom SQL deletes (metadata.getCustomSQLDelete() == null), keeping them all on Hibernate's BasicCollectionPersister alongside indexed collections. JoinTableCollectionPersisterSpec has been updated with unit tests verifying each of these collection types resolves to BasicCollectionPersister.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@testlens-app

testlens-app Bot commented Oct 25, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 75a19d6
▶️ Tests: 91989 executed
⚪️ Checks: 96/96 completed


Learn more about TestLens at testlens.app/docs.

@borinquenkid
borinquenkid merged commit 13978af into 8.0.x Oct 25, 2026
97 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Hibernate 7] unidirectional hasMany removeFrom... operation completely clears the association

3 participants