hibernate - 刷新页面时 grails 中奇怪的 GORM 行为 (F5)

标签 hibernate grails grails-orm

我有一个实体(作者)和一个呈现所有作者的 Controller 操作。

def index = {
    def list = Author.list()
    render(view: 'index', model: ['allauthors' : list])
}

渲染页面时,将按预期执行单个查询:

Hibernate: 
  select
    this_.id as id0_0_,
    this_.version as version0_0_,
    this_.name as name0_0_
  from
    author this_

但是,当我按“刷新”(F5) 时,将为每个作者执行一个选择语句(这里我有 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=?

为什么会出现这种情况???

最佳答案

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

cache.use_query_cache = true

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

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

static mapping = {
    cache true
}

关于hibernate - 刷新页面时 grails 中奇怪的 GORM 行为 (F5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293527/

相关文章:

session - org.hibernate.HibernateException 消息重新关联的对象有脏集合

grails - 在 GORM criteriaBuilder 中包含最大和偏移标准会返回错误

java - 如何使用命名查询返回 Map<key,value>?

java - Spring Hibernate 生成动态查询

java - Hibernate 全局过滤条件

java - JDBC 中的 Oracle 到 Postgresql 迁移问题

Grails .save(刷新 : true) behaves the same with . save()

Grails:创建不想存储在数据库中的类

mysql - Grails - 引导测试数据失败

grails - 如何实现软删除