同一会话/事务下的Groovy SQL + Gorm

时间:2011-02-21 01:13:19

标签: sql grails groovy gorm transactional

请仔细阅读下面的简单方案,我找不到以文本形式提问的更好方法:

两个域对象和一个事务服务:

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?
 }
}

1 个答案:

答案 0 :(得分:0)

Hibernate无法解析SQL,因此它不会“知道”您写入数据库的内容。 Hibernate不会重新加载所有会话对象 - 这肯定是巨大的开销。

也许如果您将查询重写为HQL,对象将立即可用于会话。