How to get and use a transient instance?

こ雲淡風輕ζ 提交于 2019-12-13 04:54:18

问题


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!


回答1:


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.




回答2:


You can evict from session so it will be not processed during flush



来源:https://stackoverflow.com/questions/18239292/how-to-get-and-use-a-transient-instance

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