Skip to content

Hibernate 7: write the rows of lists and maps that share a join table - #16756

Merged
borinquenkid merged 1 commit into
8.0.xfrom
fix/h7-shared-join-table-lists-8.0.x
Oct 29, 2026
Merged

borinquenkid merged 1 commit into
8.0.xfrom
fix/h7-shared-join-table-lists-8.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

Fixes #16700. Stacked on #16750 (it builds on the shared join table uniqueness of #16701 and the Db2 rules of #16750); retarget to 8.0.x once those merge.

Problem

Two unidirectional hasMany lists (or maps) to the same class that share one join table could not be saved:

NULL not allowed for column "ROLES_IDX"; SQL statement:
insert into owner_role (owner_deny_roles_id, deny_roles_idx, role_id) values (?, ?, ?)

Hibernate keys the join table of a list or a map with a primary key over its key and index columns (IndexedCollection.createPrimaryKey). On a shared table the next collection replaces it with its own, and it makes those columns NOT NULL, while a row of one list leaves the other list's columns null.

Change

SharedJoinTableUniqueKeyBinder, for each list or map of a shared table: makes its key and index columns nullable, drops that primary key, and gives it a uniqueness of its own over its key and index columns through SharedJoinTableUniqueness, the same per-database rules sets get:

Database Two lists with a key column each Two lists with one shared key column
H2, PostgreSQL, MySQL, MariaDB a unique key per list a unique key per list
Oracle, Db2 LUW a unique key per list one unique key over the key column and both index columns
Db2 for IBM i and z/OS — a create unique where not null index per list
SQL Server — a filtered unique index per list

A position or a map key is taken once; an element may repeat, as a list allows. On Oracle and Db2 LUW SharedJoinTableUniqueness now creates the key when there is none to widen, since a list has no implicit unique key (a set always has one, so sets are unchanged).

Tests

  • SharedJoinTableListHibernate7Spec (new): both shapes keep their lists in order; removing from one list keeps the other and the order of the rest; an element may repeat and clearing one list keeps the other; two maps keep their entries. A container feature writes and reads both shapes on H2, PostgreSQL, MySQL, MariaDB, Oracle and Db2 (CI, amd64), and checks that a second row at a taken position is rejected; with the uniqueness removed every database accepts it.
  • SharedJoinTableUniqueKeyBinderSpec: the DDL of two shared lists on H2, Oracle, Db2 LUW, IBM i and SQL Server (55/0).
  • Full grails-data-hibernate7-core suite locally: 4293 tests, 0 failures (the shared DatabaseContainers of Hibernate 7: support Db2 in unique key null handling, regular expressions and native SQL reading #16750 makes the Oracle rows pass locally too).

Not covered: a set that shares a join table with a list.

Docs

The shared join table note of the 8.0.x upgrade guide, with the migration a schema created by an earlier Grails 8 release needs (drop the primary key, make the index columns nullable; dbCreate: update changes neither), and the one-to-many chapter.

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.

@borinquenkid
borinquenkid marked this pull request as draft October 24, 2026 21:02
@borinquenkid
borinquenkid marked this pull request as ready for review October 24, 2026 21:02
@borinquenkid

Copy link
Copy Markdown
Member Author

CI run 40132431052 is green, with its tests run (run ci plus a ready toggle). SharedJoinTableListHibernate7Spec's container row passed on H2, Postgres, MySQL, MariaDB, Oracle and Db2 in all three Hibernate 7 test jobs that ran it.

@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. Lists and maps that share a table can be inserted, but two lists that also share the element column still load and delete each other's rows.

@borinquenkid

Copy link
Copy Markdown
Member Author

Resolved the row collision when collections share both foreign-key and element columns:

  • For lists and maps sharing join tables, SharedJoinTableUniqueKeyBinder restricts each collection using its index column (and unshared element columns, if any). This ensures that collections sharing foreign-key and element columns isolate their rows by their distinct index columns (roles_idx is not null vs deny_roles_idx is not null).
  • Loads, single-element removals, and bulk deletes now affect only the target collection's rows.
  • Added tests verifying element ordering, single-element removal, and list-clearing isolation when two lists share both key and element columns.
  • Updated documentation in the one-to-many chapter and the 8.0 upgrade guide.

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

The earlier comment is fixed. Each shared list is restricted by its own index column, clearing one keeps the other, and loading no longer selects the other list's rows. That thread is resolved. Not approving yet, only because the migration steps leave an existing table unable to take rows from the other list.

@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. The upgrade note now says to drop the primary key and make the owner-key and index columns nullable, which is what the binder does. That comment is resolved.

@borinquenkid
borinquenkid changed the base branch from fix/h7-db2-8.0.x to fix/h7-shared-join-table-key-8.0.x October 26, 2026 13:18
@jdaugherty

Copy link
Copy Markdown
Contributor

@borinquenkid Please update the upgrade skill in addition to the guide.

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

Reviewed the full diff against the stacked base at e635103. No new actionable findings in the supported shared-list and shared-map cases. The earlier row-isolation and migration comments are addressed.

Validation: ./gradlew :grails-data-hibernate7-core:test --console=plain --max-workers=4 passed with 4,311 tests, 4,292 successes, 0 failures, and 19 skipped. This run included three temporary review tests covering three lists sharing the owner and element columns during reorder/clear, HQL and criteria query isolation, and maps sharing those columns during replacement, removal, and clearing; all passed.

The PR tests also passed against H2, PostgreSQL, MySQL, MariaDB, and Oracle locally. Db2 runtime coverage was skipped on ARM; SQL Server and IBM i/z/OS behavior was checked through the existing dialect/DDL coverage rather than live databases. Mixed set/list sharing remains outside this verification.

The failing CI job reports ForwardingSpec in grails-test-examples-app1, which does not appear related to this diff.

The only ask I still have is to update the Grails Upgrade skill.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jdaugherty Updated in commit 7ca0fd6: added the shared join table behavior and schema migration instructions to the Grails 8 upgrade skill ().

@borinquenkid
borinquenkid changed the base branch from fix/h7-shared-join-table-key-8.0.x to 8.0.x October 27, 2026 03:31
@testlens-app

This comment has been minimized.

@borinquenkid
borinquenkid force-pushed the fix/h7-shared-join-table-lists-8.0.x branch from 7ca0fd6 to a62c6df Compare October 29, 2026 11:36
@borinquenkid
borinquenkid merged commit a4a8df7 into 8.0.x Oct 29, 2026
15 of 17 checks passed
@jdaugherty
jdaugherty deleted the fix/h7-shared-join-table-lists-8.0.x branch October 29, 2026 13:24
borinquenkid added a commit that referenced this pull request Nov 3, 2026
…he Groovy sources

8.0.x gained the QueryParameters/QuerySettings split of HqlQueryContext (#16800)
and the shared join table list fix (#16756) after this branch was cut from
10.0.x. Their Java sources were converted here by hand: QueryParameters and
QuerySettings are new Groovy records, HqlQueryContext, HqlQueryMethods,
SelectHqlQuery and MutationHqlQuery follow the 8.0.x change, and the Java
SharedJoinTable classes and the specs and docs of those commits apply as they
are. A merge of 8.0.x into 10.0.x would otherwise conflict on the deleted Java
files; resolve those by keeping the Groovy sources.

The ByteBuddyGroovyProxyFactorySpec feature of the proxy-walk commit (#16721)
is left out: it needs the grails-datastore-core change of that commit.

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Hibernate 7] Two unidirectional hasMany Lists sharing a join table cannot write their rows

4 participants