【问题标题】:Groovy SQL + Gorm under same session/transaction同一会话/事务下的 Groovy SQL + Gorm
【发布时间】:2011-02-21 01:13:19
【问题描述】:

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

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

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

【问题讨论】:

    标签: sql grails groovy grails-orm transactional


    【解决方案1】:

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

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

    【讨论】:

    • 是的,这似乎是实现这一目标的唯一方法。但并非所有 SQL 都可以用 HQL 轻松编写。我最好也将所有读取都转换为 SQL。 Gorm 在这里对我没用,因为我只需要编写存储过程。
    • 如果你想利用 GORM,你可以尝试为存储过程或任意 SQL 调整 Hibernate 映射:docs.jboss.org/hibernate/core/3.3/reference/en/html/…
    猜你喜欢
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多