【问题标题】:grails/GORM instance difference between domain fetched with get vs readgrails/GORM 实例与 get 与 read 获取的域之间的差异
【发布时间】:2018-10-18 18:22:22
【问题描述】:

我知道 DomainClass.get() 和 DomainClass.read() 之间的功能区别。当属性发生变化时,“读取”会在会话刷新时停止休眠自动保存实例。

但我找不到两个返回的实例之间有什么区别 改变行为。

Grails 为域实例提供了一个属性“instanceGormInstanceApi”,我认为它必须包含答案,但到目前为止我还无法弄清楚该对象的区别。

【问题讨论】:

    标签: hibernate grails grails-orm


    【解决方案1】:

    经过进一步调查,只读行为与实例所在的会话挂钩。

    这相当于使对象“读取”:

    DomainClass.withSession{session->
        session.setReadOnly(myInstance,true)
        session.setFlushMode(FlushMode.MANUAL)//this isn't always necessary?
    }
    

    请参阅 GrailsHibernateUtils 中的源代码方法 setObjectToReadOnly https://github.com/grails/gorm-hibernate5/blob/master/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsHibernateUtil.java

    “读取”的不幸限制是它不会影响关系。因此,如果您让 Parent 读取,Child 对象仍将自动保存并独立处理。来自 grails 文档...

     There is one exception to this though - any associated collections, for 
     example an Author's books collection, will participate in automatic 
     flushing and dirty detection. 
    

    【讨论】:

      猜你喜欢
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多