Grails / GORM, disable first level cache

Suppose I have the following domain class mapping to an obsolete table, using a 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, DomainObject links are split due to first-level caching, and thus the transition fields write over one another. For instance,

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 table data without link exchange. Any ideas?

[change]

Understanding the situation better now, I believe that my question boils down to the following: is there anyway to disable the first level cache for a particular domain class when using the second level cache?

[change]

, , , .

+3
3

, , .

( ):

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
+2

, BootStrap get() ( ), - get() .

, , ? Hibernate . Hibernate, .

0

Instead of DomainObject.get (1) you can use DomainObject.findById (1). Because the get method caches the result of the request, but the first does not.

0
source

Source: https://habr.com/ru/post/1746029/


All Articles