问题
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