刷新页面时Grails中出现奇怪的GORM行为(F5)

geo*_*geo 5 grails hibernate grails-orm

我有一个实体(作者)和一个控制器动作,呈现所有作者.

def index = {
    def list = Author.list()
    render(view: 'index', model: ['allauthors' : list])
}
Run Code Online (Sandbox Code Playgroud)

呈现页面时,将按预期执行单个查询:

Hibernate: 
  select
    this_.id as id0_0_,
    this_.version as version0_0_,
    this_.name as name0_0_
  from
    author this_
Run Code Online (Sandbox Code Playgroud)

但是,当我按下Refresh(F5)然后为每个作者执行一个select语句(这里我有3个作者):

Hibernate: 
select
    author0_.id as id0_0_,
    author0_.version as version0_0_,
    author0_.name as name0_0_
from
    author author0_ 
where
    author0_.id=?
Hibernate: 
select
    author0_.id as id0_0_,
    author0_.version as version0_0_,
    author0_.name as name0_0_
from
    author author0_ 
where
    author0_.id=?
Hibernate: 
select
    author0_.id as id0_0_,
    author0_.version as version0_0_,
    author0_.name as name0_0_
from
    author author0_ 
where
    author0_.id=?
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

Kel*_*lly 1

看起来这与查询缓存有关。如果你有

cache.use_query_cache = true
Run Code Online (Sandbox Code Playgroud)

在您的 Datasource.groovy 中,但没有在域类中设置缓存,缓存似乎会逐出每个条目上的所有条目list(),并且必须重新缓存每个条目(只是猜测)。

如果您向域添加缓存,则多个选择就会消失 - 事实上,当我在添加以下内容后刷新时,不会执行任何选择:

static mapping = {
    cache true
}
Run Code Online (Sandbox Code Playgroud)