borinquenkid opened a new issue, #16700:
URL: https://github.com/apache/grails-core/issues/16700
### Expected Behavior
An owner with two unidirectional `hasMany` **Lists** (or Maps) to the same
class that share one join table
```groovy
class Owner {
List<Role> roles
List<Role> denyRoles
static hasMany = [roles: Role, denyRoles: Role]
}
```
writes an element added to either list to the shared join table and reads it
back.
### Actual Behaviour
The shared table gets the columns `roles_idx`, `deny_roles_idx`,
`owner_roles_id`, `owner_deny_roles_id`, `role_id`. Hibernate's
`IndexedCollection.createPrimaryKey` creates a primary key per list over that
list's key and index columns, and the second list's second pass **overwrites**
the first list's primary key (`Table.setPrimaryKey`), so the table ends up with
`PRIMARY KEY (deny_roles_idx, owner_deny_roles_id)` only, and both index
columns are NOT NULL. A row of either list leaves the other list's index column
null, so the insert fails:
```
NULL not allowed for column "ROLES_IDX"; SQL statement:
insert into owner_role (owner_deny_roles_id,deny_roles_idx,role_id) values
(?,?,?)
```
Until the fix for #16699 the classic binder made the keys of such lists not
updatable, so Hibernate 7 wrote no rows at all and the elements were lost
silently. With the keys updatable the defect surfaces as a
`DataIntegrityViolationException` on save instead, which is the honest failure
but not the expected behaviour.
### Analysis
A shared join table of indexed collections needs, per list, a nullable index
column and no primary key that spans one list's columns only: for example a
unique key per list over its key column, its index column and the element
column, as #16699 does for sets with the single implicit unique key. Giving
each list its own join table by default would also fix it but changes the
schema of existing applications.
### Steps To Reproduce
Register the classes above against the Hibernate 7 datastore of 8.0.x with
the fix for #16699, add a role to each list, flush: the second insert fails
with the error above. Without that fix both lists reload empty.
### Environment Information
- Grails 8.0.x, `grails-data-hibernate7`, classic domain binding.
Related: #16699, #16665, #10811.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]