Should `getPersistentValue` work for collections?
grails, grails-orm
Solution
`getPersistentValue` does not go to the database. When Hibernate creates a domain class instance, it keep a copy of the original data and that's what is used for the `isDirty` check and `getPersistentValue`. Hibernate uses the data when flushing to do a field-by-field comparison of the current state against the original state to determine if anything should be pushed to the database.
A collection is different however. It's not a simple `HashSet`/`ArrayList` - it's a change-aware `PersistentSet`/`PersistentList`. But it doesn't track what changed, just that something changed. So there's nothing to compare to find the previous state without going to the database.
The big issue is that for small collections, you could implement something sensible for this. But in the general case you have no idea how large the collection could be and it could have a significant number of elements, so doing this in-memory would be very expensive.
Problem
If `Team` `hasMany = [players: Player]` and `Player` `belongsTo = [team: Team]`, should `team.getPersistentValue('players')` return the list of `players` from the database (as described in the guide)? Or does this only work for non-collection properties? For me it returns the same list as `team.players` even though I updated the list using `addToPlayers` and `removeFromPlayers` (Grails 2.3.4).