Groovy SQL +同一会话/事务下的 Gorm
请完成下面的简单场景,我找不到更好的方法以文本形式提出问题:
两个域对象和一个事务服务:
A {
int id
String prop1
B b
static constraints = {b nullable:true}
}
B {
int id
String prop1
// not worring about belongsTo here
}
SomeService {
def transactional = true
def sessionFactory
def aTransactionalMethod() {
Sql sql = new Sql(sessionFactory.currentSession.connection())
sql# create A some how with sql query leaving property b as null.
A a = A.findById(...)
//a.b must be null here, never mind
sql# create B object somehow with sql query.
// should a.b be available now? I'm getting null here.. session.currentSession.refresh(a) resolves the issue but why is that?
}
}
Please go through the simple scenario below, I couldn't find a better way to ask the question in textual form:
Two domain objects and a transactional service:
A {
int id
String prop1
B b
static constraints = {b nullable:true}
}
B {
int id
String prop1
// not worring about belongsTo here
}
SomeService {
def transactional = true
def sessionFactory
def aTransactionalMethod() {
Sql sql = new Sql(sessionFactory.currentSession.connection())
sql# create A some how with sql query leaving property b as null.
A a = A.findById(...)
//a.b must be null here, never mind
sql# create B object somehow with sql query.
// should a.b be available now? I'm getting null here.. session.currentSession.refresh(a) resolves the issue but why is that?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hibernate 无法解析 SQL,因此它不“知道”您向数据库写入的内容。 Hibernate 不会重新加载所有会话对象——这肯定会是巨大的开销。
也许如果您将查询重写为 HQL,对象将立即可供会话使用。
Hibernate can't parse SQL, so it doesn't "know" what you write to database. Hibernate will not reload all session objects - that would definitely be huge overhead.
Maybe if you rewrite your query to HQL the objects will become available to session in immediately.