gpt4 book ai didi

hibernate - Grails 3-防止 Controller 退出后不必要的选择语句

转载 作者:行者123 更新时间:2023-12-02 15:49:28 25 4
gpt4 key购买 nike

请原谅人为的例子。我只是不知道为什么会发生这些选择。

域对象:

class Author {
String name
Location location

static mapping = {
location lazy: true //this is default, but set here to reduce confusion
}

static constraints = {
}
}

...
class Location {
String address
static hasOne = [longLat : LongLat]

static constraints = {
}
}

...
class LongLat {
String longitude
String latitude

static belongsTo = [location:Location]

static constraints = {
}
}

...

bootstrap 初始化:
def init = { servletContext ->
Location loc = new Location(address: '123 asdf dr', longLat: new LongLat(longitude: 0.5, latitude: 0.5)).save(flush:true)
new Author(name: 'Author Name', location: loc).save(flush:true)
}

...

来自 Controller 的 Action :
def index() {
println "Start Controller"
Author.get(1)
render '1'
println "End Controller"
}

我已启用日志记录:
logSql: true
formatSql: true

输出:
Grails application running at http://localhost:8080 in environment: development
Start Controller
Hibernate:
select
this_.id as id1_0_0_,
this_.version as version2_0_0_,
this_.location_id as location3_0_0_,
this_.name as name4_0_0_
from
author this_
where
this_.id = ?
End Controller
Hibernate:
select
location0_.id as id1_1_0_,
location0_.version as version2_1_0_,
location0_.address as address3_1_0_
from
location location0_
where
location0_.id=?
Hibernate:
select
longlat0_.id as id1_2_0_,
longlat0_.version as version2_2_0_,
longlat0_.latitude as latitude3_2_0_,
longlat0_.location_id as location4_2_0_,
longlat0_.longitude as longitud5_2_0_
from
long_lat longlat0_
where
longlat0_.location_id=?

为什么最后两个选择会发生,如何在不诉诸HQL的情况下停止它们?

我正在使用Grails 3.2.3。

最佳答案

根据我们在评论中的对话,事实证明它很可能与OSIV( View 中的公开 session )有关。解决方案是使用.discard()来确保休眠状态不会检查是否有任何更改(例如,脏检查)并且需要持久化。

关于hibernate - Grails 3-防止 Controller 退出后不必要的选择语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com