简体   繁体   English

grails / GORM实例使用get vs read读取的域之间的差异

[英]grails/GORM instance difference between domain fetched with get vs read

I know the functional difference between DomainClass.get() and DomainClass.read(). 我知道DomainClass.get()和DomainClass.read()之间的功能差异。 "Read" stops hibernate from auto saving instances at session flush when properties have changed. 属性更改后,“读取”会在会话刷新时停止从自动保存实例的休眠状态。

But I can't find what the difference is between the two returned instances that changes the behavior. 但是我找不到改变行为的两个返回实例之间的区别。

Grails gives domain instances a property "instanceGormInstanceApi", which I assume must hold the answer, but so far I have not been able to figure ou the difference in that object. Grails为域实例提供了一个属性“ instanceGormInstanceApi”,我认为该属性必须包含答案,但是到目前为止,我仍无法弄清楚该对象的区别。

After further investigation, the read only behaviour is hooked to the session the instance is in. 经过进一步调查后,只读行为将挂接到实例所在的会话。

This is the equivalent of what makes the object "read": 这等效于使对象“读取”:

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

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

The unfortunate limitations of "read" is that is does not affect relationships. “读取”的不幸局限性是它不会影响关系。 So if you make Parent read, the Child objects will still be saved automatically and will be treated independently. 因此,如果让“父母”阅读,“孩子”对象仍将自动保存并被独立对待。 From the grails docs... 从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. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM