如何获取和使用瞬态实例? [英] How to get and use a transient instance?
问题描述
我只是想在控制器中使用一个域类的临时实例。瞬态实例本质上是两个持久实例的交叉产物,我不需要或不想坚持它。但是,我仍然得到一个TransientObjectException。有没有办法实例化一个对象的一点点,只是为了方便,然后扔掉它没有坚持它?
这是grails 2.2.0。感谢!
好的,添加了一些代码:
我处理的是保修
:
class保修{
// ...其他字段...
客户端客户端
客户
是唯一必填字段。 客户端
类同样具有保证
外键:
class客户端{
// ...其他字段...
保修保修
在控制器中:
String name = params .name
if(name == null){return}
Client client = Client.findByClientName(name as String)
... ...其他东西...
def warranty =新保修(客户:客户)
返回//用于测试目的
...并引发异常!
解决方案我的猜测是您修改了客户端实例,因此当您创建新的Warranty时,它会引用未保存的客户端。尝试添加
client.discard()
在创建保修期之前。I'm just trying to use a transient instance of a domain class in a controller. The transient instance is essentially a cross-product of two persistent instances, and I don't need or want to persist it. However, I am still getting a TransientObjectException. Is there no way to instantiate an object for a little bit just for convenience and then throw it away without persisting it?
This is grails 2.2.0. Thanks!
Okay, adding some codes:
The class in particular that I'm dealing with is
Warranty
:class Warranty { // ...other fields... Client client
client
is the only required field. TheClient
class, similarly, has aWarranty
foreign key:class Client { // ...other fields... Warranty warranty
In the controller:
String name = params.name if (name == null) { return } Client client = Client.findByClientName(name as String) // ...other stuff... def warranty = new Warranty(client: client) return // for testing purposes
...and that raises the exception!
解决方案My guess is that you modified your client instance, so when you create a new Warranty, it references to the unsaved client.
Try adding
client.discard()
before creating the warranty.这篇关于如何获取和使用瞬态实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!