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

发布于 2024-10-18 05:43:59 字数 693 浏览 1 评论 0原文

请完成下面的简单场景,我找不到更好的方法以文本形式提出问题:

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2024-10-25 05:43:59

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文