Grails / GORM, read-only cache and transient fields
- by Stephen Swensen
Suppose I have the following Domain object mapping to a legacy table, utilizing read-only second-level cache, and having a transient field:
class DomainObject {
static def transients = ['userId']
Long id
Long userId
static mapping = {
cache usage: 'read-only'
table 'SOME_TABLE'
}
}
I have a problem, references to DomainObject instances seem to be shared due to the caching, and thus transient fields are writing over each other. For example,
def r1 = DomainObject.get(1)
r1.userId = 22
def r2 = DomainObject.get(1)
r2.userId = 34
assert r1.userId == 34
That is, r1 and r2 are references to the same instance. This is undesirable, I would like to cache the table data without sharing references. Any ideas?