简体   繁体   中英

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

I know the functional difference between DomainClass.get() and 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.

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

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...

 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. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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