开发者

Groovy SQL + Gorm under same session/transaction

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜