如何获取和使用瞬态实例? [英] How to get and use a transient instance?

查看:155
本文介绍了如何获取和使用瞬态实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在控制器中使用一个域类的临时实例。瞬态实例本质上是两个持久实例的交叉产物,我不需要或不想坚持它。但是,我仍然得到一个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. The Client class, similarly, has a Warranty 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屋!

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