Get a list of all objects grails is planning to magically save

我怕爱的太早我们不能终老 提交于 2020-01-03 01:59:08

问题


If I make a domain object in a controller and don't call .save(), Grails will do it for me automatically at some point. I am creating lots of domain objects without planning to save all of them and getting 'references an unsaved transient instance' exceptions when my service exits.

How would I get a list of all objects that Grails will try to save when the controller/service exits so that I could prevent some of them from being saved?


回答1:


You're better off retrieving instances that you know you will edit but don't want persisted with the read method instead of get. Using read doesn't make it read-only, since if you call save() and it was modified it will be persisted, but it won't be auto-persisted when the OSIV interceptor flushes the session.

Another option (especially if the instance isn't individually loaded) is to remove it from the session using detach() while you're editing it.

You could also use DTOs and copy the data into those non-persistent classes so you're not mucking with persistent classes, just data classes that Hibernate doesn't know about.



来源:https://stackoverflow.com/questions/11944004/get-a-list-of-all-objects-grails-is-planning-to-magically-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!