假设我具有以下到旧表的Domain类映射,利用只读二级缓存并具有一个临时字段:

class DomainObject {
 static def transients = ['userId']

 Long id
 Long userId

 static mapping = {
  cache usage: 'read-only'
  table 'SOME_TABLE'
 }
}

我有一个问题,由于第一级缓存而共享了对DomainObject的引用,因此 transient 字段相互覆盖。例如,
def r1 = DomainObject.get(1)
r1.userId = 22

def r2 = DomainObject.get(1)
r2.userId = 34

assert r1.userId == 34

即,r1和r2是对同一实例的引用。这是不可取的,我想在不共享引用的情况下缓存表数据。有任何想法吗?

[编辑]

现在更好地了解情况了,我认为我的问题归结为以下几点:是否仍然在使用二级缓存的同时禁用特定域类的一级缓存?

[编辑]

由于似乎没有干净的方法来实现此目标,因此我们选择围绕它的需要进行重新设计。

最佳答案

请忽略我之前的回答,我对您的问题不完全理解。

但是,以下将起作用(经过代码测试):

def r1 = DomainObject.get(1)
r1.userId = 22
r1.discard() //BE CAREFUL WITH THIS, YOU MIGHT END UP WITH a LazyInitializationException

def r2 = DomainObject.get(1)
r2.userId = 34

assert r1.userId == 22

10-07 21:12