apache / apache/grails-core

isDirty on many-to-many relationship doesn't work

Open
#12,149 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I would like to resurrect a previously reported bug...

https://github.com/grails/grails-core/issues/11321

Because I for one can't see how there is not some kind of bug here.

According to the documentation http://gorm.grails.org/6.0.x/hibernate/manual/#modificationChecking

"isDirty() does not currently check collection associations, but it does check all other persistent properties and associations."

But the documentation doesn't tell you how to make these associations save, since apparently it can't detect changes automatically.

```
Person person = Person.findByName('alber2')
Position position = Position.findByName('boss2')
person.addToPositions(position)
```

This does not save the new relationship.

In the 1:M case, lack of auto dirty checking doesn't seem such a problem because one of the visible tables will have changed and will be dirty. But in the M:M case, the table that needs to change is the invisible table position_person that's automatically populated.

One can't call markDirty() on the intervening position_person table, because it's invisible to the programmer in the infrastructure.

I've tried marking the owning table and subordinate tables as dirty:

Person person = Person.findByName('alber2')
Position position = Position.findByName('boss2)
person.addToPositions(position)
position.markDirty()
person.markDirty()
But that doesn't work. person.isDirty() still returns false, and the relationship doesn't save.

How is this supposed to work? How can I get the relationship to save?

- **GORM Version:** 6.1.12
- **Grails Version (if using Grails):** 3.3.14
- **JDK Version:** 1.8

### Example Application

There is an example app here https://github.com/grails/grails-core/issues/11321

Contributor guide

Open the contributing guide

Research direction

Reproduce the many-to-many case from the issue using Person.findByName, Position.findByName, addToPositions, and markDirty() with GORM 6.1.12 and Grails 3.3.14; inspect the linked issue 11321 and its example application first. Done means the relationship change persists as expected and the behavior around isDirty() is covered or clearly documented.

Written by the indexing model from the issue text.

Assessment

Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.