You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@grails.apache.org by "ianroberts (via GitHub)" <gi...@apache.org> on 2026/10/17 18:07:15 UTC

[I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

ianroberts opened a new issue, #16665:
URL: https://github.com/apache/grails-core/issues/16665

   ### Expected Behavior
   
   Given a set of domain classes like the following:
   
   ```groovy
   // Role.groovy
   class Role {
     String authority
   }
   
   // Tag.groovy
   class Tag {
     String name
     String description
   }
   
   // Item.groovy
   class Item {
     String name
   
     static hasMany = [roles: Role, tags: Tag]
   }
   ```
   
   the following sequence of method calls should 1. create an item, 2. create a role, 3. add a row to the `item_role` join table linking the two
   
   ```groovy
   def item = new Item(name: "Example item")
   item.save(flush: true)
   def role = new Role(authority: "ROLE_EXAMPLE")
   item.addToRoles(role)
   item.save(flush: true)
   ```
   
   ### Actual Behaviour
   
   With Hibernate 5 the join table insert happens as expected, but with Hibernate 7 it does not - the `Item` and `Role` are created and the second `item.save` issues the expected SQL `UPDATE` to the `item` table, but it does _not_ persist the `addToRoles` in the join table.
   
   If I remove the `tags` entry from `hasMany` (so the `Item` class only has _one_ unidirectional one-to-many association rather than two) then the correct behaviour is observed on both Hibernate versions.
   
   ### Steps To Reproduce
   
   See my [example repository](https://github.com/ianroberts/grails-h7-multiple-hasmany) - this has two Grails applications that are identical except that one uses Hibernate 5 and the other uses Hibernate 7.  They declare two versions of the `Item` class, one as above with `static hasMany = [roles: Role, tags: Tag]` and the other (`UntaggedItem`) with just `static hasMany = [roles: Role]`, and perform the above steps in `BootStrap` with Hibernate SQL logging enabled
   
   - create the item
   - `findOrCreate` the role
   - `addToRoles`
   - save and flush
   
   On Hibernate 5 both `Item` and `UntaggedItem` log the correct join table inserts
   
   ```
   2026-10-17T19:00:35.544+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : ======== Creating Item =========
   2026-10-17T19:00:35.628+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : insert into item (id, version, name) values (default, ?, ?)
   2026-10-17T19:00:35.652+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : Created com.example.Item : 1
   2026-10-17T19:00:35.653+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : findOrCreate example role
   2026-10-17T19:00:35.658+01:00  WARN 1214 --- [  restartedMain] org.hibernate.orm.deprecation            : HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA jakarta.persistence.criteria.CriteriaQuery instead
   2026-10-17T19:00:35.676+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : select this_.id as id1_3_0_, this_.version as version2_3_0_, this_.authority as authorit3_3_0_ from role this_ where (this_.authority=?) limit ?
   2026-10-17T19:00:35.679+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : Add role to item
   2026-10-17T19:00:35.680+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : Save and flush
   2026-10-17T19:00:35.684+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : insert into role (id, version, authority) values (default, ?, ?)
   2026-10-17T19:00:35.736+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : update item set version=?, name=? where id=? and version=?
   2026-10-17T19:00:35.738+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : insert into item_role (item_roles_id, role_id) values (?, ?)
   2026-10-17T19:00:35.740+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : ======== Creating UntaggedItem =========
   2026-10-17T19:00:35.742+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : insert into untagged_item (id, version, name) values (default, ?, ?)
   2026-10-17T19:00:35.743+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : Created com.example.UntaggedItem : 1
   2026-10-17T19:00:35.743+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : findOrCreate example role
   2026-10-17T19:00:35.743+01:00  WARN 1214 --- [  restartedMain] org.hibernate.orm.deprecation            : HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA jakarta.persistence.criteria.CriteriaQuery instead
   2026-10-17T19:00:35.744+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : select this_.id as id1_3_0_, this_.version as version2_3_0_, this_.authority as authorit3_3_0_ from role this_ where (this_.authority=?) limit ?
   2026-10-17T19:00:35.745+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : Add role to item
   2026-10-17T19:00:35.745+01:00  INFO 1214 --- [  restartedMain] com.example.BootStrap                    : Save and flush
   2026-10-17T19:00:35.746+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : update untagged_item set version=?, name=? where id=? and version=?
   2026-10-17T19:00:35.746+01:00 DEBUG 1214 --- [  restartedMain] org.hibernate.SQL                        : insert into untagged_item_role (untagged_item_roles_id, role_id) values (?, ?)
   ```
   
   On Hibernate 7 only the `UntaggedItem` logs the join table inserts:
   
   ```
   2026-10-17T19:01:52.908+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : ======== Creating Item =========
   2026-10-17T19:01:52.989+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : insert into item (name,version,id) values (?,?,default)
   2026-10-17T19:01:53.019+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : Created com.example.Item : 1
   2026-10-17T19:01:53.019+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : findOrCreate example role
   2026-10-17T19:01:53.120+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : select r1_0.id,r1_0.authority,r1_0.version from role r1_0 where r1_0.authority=?
   2026-10-17T19:01:53.129+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : Add role to item
   2026-10-17T19:01:53.130+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : Save and flush
   2026-10-17T19:01:53.135+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : insert into role (authority,version,id) values (?,?,default)
   2026-10-17T19:01:53.157+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : update item set name=?,version=? where id=? and version=?
   2026-10-17T19:01:53.162+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : ======== Creating UntaggedItem =========
   2026-10-17T19:01:53.164+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : insert into untagged_item (name,version,id) values (?,?,default)
   2026-10-17T19:01:53.165+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : Created com.example.UntaggedItem : 1
   2026-10-17T19:01:53.165+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : findOrCreate example role
   2026-10-17T19:01:53.166+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : select r1_0.id,r1_0.authority,r1_0.version from role r1_0 where r1_0.authority=?
   2026-10-17T19:01:53.166+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : Add role to item
   2026-10-17T19:01:53.167+01:00  INFO 1446 --- [  restartedMain] com.example.BootStrap                    : Save and flush
   2026-10-17T19:01:53.168+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : update untagged_item set name=?,version=? where id=? and version=?
   2026-10-17T19:01:53.169+01:00 DEBUG 1446 --- [  restartedMain] org.hibernate.SQL                        : insert into untagged_item_role (untagged_item_roles_id,role_id) values (?,?)
   ```
   
   and you can verify with the scaffolded `ItemController` whether the associations have been saved.
   
   > Note: the test is one `hasMany` vs two, but I am seeing the same issue in my real app for classes that have three different `hasMany` links, so it does look like an issue of one vs more-than-one, rather than two vs not-two
   
   ### Environment Information
   
   - Operating system: macOS Tahoe 26.6.2
   - JDK: Azul Zulu JDK 21
   
   ```
   openjdk version "21.0.12.1" 2026-08-18 LTS
   OpenJDK Runtime Environment Zulu21.52+203-CA (build 21.0.12.1+1-LTS)
   OpenJDK 64-Bit Server VM Zulu21.52+203-CA (build 21.0.12.1+1-LTS, mixed mode, sharing)
   ```
   
   ### Example Application
   
   https://github.com/ianroberts/grails-h7-multiple-hasmany
   
   ### Version
   
   8.0.0


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

Posted by "ianroberts (via GitHub)" <gi...@apache.org>.
ianroberts commented on issue #16665:
URL: https://github.com/apache/grails-core/issues/16665#issuecomment-6238814353

   Sample app is Grails 8.0.0 but I've just confirmed the same behaviour on 8.1.0-SNAPSHOT and 9.0.0-SNAPSHOT as well.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

Posted by "ianroberts (via GitHub)" <gi...@apache.org>.
ianroberts commented on issue #16665:
URL: https://github.com/apache/grails-core/issues/16665#issuecomment-6241794230

   In fact it's not really about whether there are two (or more) `hasMany` references to the same other side, the critical thing in that issue is about whether there are two `hasMany` links that use _the same join table_.  It should be OK to have two unidirectional `hasMany` even to the same other side domain class, as long as they are configured to use different join tables in the mapping closure.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

Posted by "ianroberts (via GitHub)" <gi...@apache.org>.
ianroberts commented on issue #16665:
URL: https://github.com/apache/grails-core/issues/16665#issuecomment-6239812713

   Digging a bit deeper, it seems to come down to this bit of code in `CollectionKeyColumnUpdater`:
   
   https://github.com/apache/grails-core/blob/ba72113c99c0df8823b4560903466ab834af423e/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java#L42-L55
   
   which specifically marks the key as `updatable = false` when the class has more than one unidirectional one-to-many property.  The referenced issue #10811 was specifically about multiple `hasMany` properties _referencing the same domain_, but the code catches _any_ case of multiple `hasMany`, whether they're referencing the same or different domain classes on the other side.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

Posted by "ianroberts (via GitHub)" <gi...@apache.org>.
ianroberts commented on issue #16665:
URL: https://github.com/apache/grails-core/issues/16665#issuecomment-6286205779

   > One point from this thread is not changed by #16686: when two properties do share a join table, the key stays non-updatable, so an element added to one of those is still not written. Whether that case should fail at startup instead is a separate decision.
   
   Given the foreign key columns are no longer created as `not null` on hibernate 7, it would be an interesting test to disable this non-updatable key logic entirely and then see what breaks. Given that (presumably) queries on the join table for a unidirectional `hasMany` will always include something like `where item_roles_id = ?`, it may be perfectly fine to use one join table for both associations with one of the columns always null.
   
   It'd be good to add a db level unique constraint for both associations (not just the first declared one, as at present), but other than that it may be that this logic can be removed altogether.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

Posted by "ianroberts (via GitHub)" <gi...@apache.org>.
ianroberts commented on issue #16665:
URL: https://github.com/apache/grails-core/issues/16665#issuecomment-6249426034

   If I change the `Item` class to have two associations to the same other side and no further mapping:
   
   ```groovy
   class Item {
     String name
   
     static hasMany = [roles: Role, denyRoles: Role]
   }
   ```
   
   then both 5 and 7 create a single join table for both associations, albeit with slightly different constraints:
   
   ```sql
   --- Hibernate 5
   create table item_role (item_roles_id bigint not null, role_id bigint, item_deny_roles_id bigint not null)
   alter table item_role add constraint FKmr27g8c794t0765o17bpi1exh foreign key (role_id) references role
   alter table item_role add constraint FKskq0ajk4g23s1rdrrao5er4qj foreign key (item_roles_id) references item
   alter table item_role add constraint FKay92y9xyiu5yaoyk89plm3h9v foreign key (item_deny_roles_id) references item
   
   --- Hibernate 7
   create table item_role (item_deny_roles_id bigint, item_roles_id bigint, role_id bigint, unique (item_roles_id, role_id))
   alter table if exists item_role add constraint FKmr27g8c794t0765o17bpi1exh foreign key (role_id) references role
   alter table if exists item_role add constraint FKskq0ajk4g23s1rdrrao5er4qj foreign key (item_roles_id) references item
   alter table if exists item_role add constraint FKay92y9xyiu5yaoyk89plm3h9v foreign key (item_deny_roles_id) references item
   ```
   
   In H5 the `Item` side columns are both `not null`.  In H7 they are nullable, and there's a `unique` constraint on `item_roles_id` (the first declared `hasMany`) but not on the `item_deny_roles_id` (the second one).
   
   Running the test app on Hibernate 5, any attempt to save an `Item` after `addToRoles` or `addToDenyRoles` throws an exception because of the `not null` on the foreign key column representing the other association
   
   ```
   insert into item_role (item_roles_id, role_id) values (?, ?)
   SQL Error: 23502, SQLState: 23502
   NULL not allowed for column "ITEM_DENY_ROLES_ID"; SQL statement:
     insert into item_role (item_roles_id, role_id) values (?, ?) [23502-240]
   ```
   
   On Hibernate 7 there is no attempt to write to the join table at all (since there are two unidirectional `hasMany` associations on the same class) so it fails silently rather than with an exception.
   
   ---
   
   Adding explicit join table mappings:
   
   ```groovy
   class Item {
     String name
   
     static hasMany = [roles: Role, denyRoles: Role]
   
     static mapping = {
       roles joinTable: 'item_allow_roles'
       denyRoles joinTable: 'item_deny_roles'
     }
   }
   ```
   
   both versions create two separate join tables:
   
   ```sql
   -- Hibernate 5
   create table item_allow_roles (item_roles_id bigint not null, role_id bigint)
   create table item_deny_roles (item_deny_roles_id bigint not null, role_id bigint)
   
   -- Hibernate 7
   create table item_allow_roles (item_roles_id bigint, role_id bigint, unique (item_roles_id, role_id))
   create table item_deny_roles (item_deny_roles_id bigint, role_id bigint, unique (item_deny_roles_id, role_id))
   ```
   
   and now Hibernate 5 succeeds in creating an item with one allowed role and no denied, but Hibernate 7 silently fails as before (because there are two unidirectional `hasMany` associations).
   
   ---
   
   ## Analysis
   
   The whole check in `CollectionKeyColumnUpdater` for "does the containing class have more than one unidirectional `hasMany`" only seems to protect against a condition that would fail at runtime anyway under Hibernate 5.  The original issue #10811 was to do with an app upgraded from Grails version 2 to version 3 nine years ago, so maybe it's time to stop trying for backwards compatibility that far back.  If the user needs compatibility with an existing database that models both associations in a single table then they can introduce an explicit class to represent the join table with many-to-one associations on the three (or more) sides.
   
   For v8.0.x we could just drop the whole "more than one `hasMany`" test altogether and accept that it's a bit of a foot-gun if you declare two `hasMany` links to the same far side class _without_ explicitly naming the join tables.  And document this more clearly in the GORM documentation that if you want to `hasMany` twice to the same class then you _need_ the explicit mapping.
   
   Then for v8.1 or v9 we could consider making it a hard error to have two `hasMany` links that use the same join table, so such cases fail up front at startup time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

Posted by "borinquenkid (via GitHub)" <gi...@apache.org>.
borinquenkid commented on issue #16665:
URL: https://github.com/apache/grails-core/issues/16665#issuecomment-6282941816

   Root cause and fix are in #16686 (targets 8.0.x, so 8.0.1).
   
   `CollectionKeyColumnUpdater` set the key of every entity collection to non-updatable when the owner had two or more unidirectional to-many properties. That is the workaround for #10811, where two `hasMany` to the same class share one join table. Hibernate writes no join table rows for a collection whose key is not updatable, so the `addTo*` of a collection with a join table of its own was dropped on flush, as reported here.
   
   The key is now non-updatable only when another unidirectional to-many property of the owner uses the same collection table. The class is identical on 8.1.x, 9.0.x, 9.1.x and 10.0.x, so it should merge forward through them. The Groovy conversion of the Hibernate 7 modules (#16572) carries a hand-ported version of the same change.
   
   One point from this thread is not changed by #16686: when two properties do share a join table, the key stays non-updatable, so an element added to one of those is still not written. Whether that case should fail at startup instead is a separate decision.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables [grails-core]

Posted by "borinquenkid (via GitHub)" <gi...@apache.org>.
borinquenkid closed issue #16665: [Hibernate 7] Multiple unidirectional hasMany in same class does not insert into join tables
URL: https://github.com/apache/grails-core/issues/16665


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org