Grails / GORM, Disable First-level Cache

Posted by Stephen Swensen on Stack Overflow See other posts from Stack Overflow or by Stephen Swensen
Published on 2010-05-18T17:04:11Z Indexed on 2010/05/19 2:10 UTC
Read the original article Hit count: 303

Filed under:
|
|
|

Suppose I have the following Domain class 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 are being shared due to first-level 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?

[Edit]

Understanding the situation better now, I believe my question boils down to the following: Is there anyway to disable first level cache for a specific domain class while still using second level cache?

© Stack Overflow or respective owner

Related posts about grails

Related posts about gorm