获取所有对象的列表,Grails计划以奇迹般的方式保存 [英] Get a list of all objects grails is planning to magically save

查看:93
本文介绍了获取所有对象的列表,Grails计划以奇迹般的方式保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在控制器中创建一个域对象,并且不要调用 .save(),那么Grails会在某些时候自动为我执行此操作。我创建了大量的域对象,但没有计划保存所有这些对象,并在我的服务退出时获取'引用未保存的瞬态实例'异常。



如何获取列表Grails将在控制器/服务退出时尝试保存的所有对象的所有对象,以便我可以防止其中一些被保存? 解决方案

你最好检索你知道你会编辑的实例,但不想用 read 方法而不是 get 持久化C $ C>。使用 read 不会使其成为只读的,因为如果您调用 save()并将其修改,它会被持久化,但是当OSIV拦截器刷新会话时它不会被自动保留。



另一个选项(特别是如果实例没有单独加载的话)是在编辑它时使用 detach()从会话中删除它。



您也可以使用DTO和将数据复制到这些非持久化类中,这样您就不会留下持久化类,只是Hibernate不知道的数据类。


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?

解决方案

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.

这篇关于获取所有对象的列表,Grails计划以奇迹般的方式保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆