apache / apache/grails-core

save() on association does not cascade in 4.x/5.x

Open
#14,627 0 comments 0 reactions 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

Please see the following test for grails-datastore-gorm-test: [gist SaveCascadeSpec.groovy](https://gist.github.com/yvesf/8560eacf42e40ba0cced95b5951d9888)

It contains two cases for two relation types:

- `ToOne`: This one fails in gorm 4.x branch and 5.x branch. It looks like a incomplete logic in NativeEntryEntityPersister
- `OneToMany`: This one succeeds in gorm 4.x, it fails in 5.x branch.

As it is shown in the test, I assume that a entity with a association where the other end `belongsTo` should cascade on save. This is what I understood from the 'cascade' help in the docs.

My idea to fix the `ToOne` is to not only check for empty id but also do the dirty check in the NativeEntryEntityPersister:

```groovy
// else if (prop instanceof ToOne) { ...
if (tempId == null) {
if (association.isOwningSide()) {
tempId = session.persist(associatedObject);
}
} else if (associatedObject instanceof DirtyCheckable) {
if (((DirtyCheckable)associatedObject).hasChanged()) {
tempId = associationPersister.persist(associatedObject);
}
}
```

For the `OneToMany` I have currently no idea how to fix it. I just realized that in the NativeEntityPersister is only checking if the PersistentSet is dirty. This is not the case, it's the entry inside the PersistentSet that is dirty.

Contributor guide

Open the contributing guide

Research direction

Start with the SaveCascadeSpec.groovy test in the linked gist, then inspect NativeEntryEntityPersister and NativeEntityPersister, especially their association and PersistentSet dirty checks. Done means the ToOne and OneToMany cases pass in the GORM 4.x and 5.x branches with the expected belongsTo cascade behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.