Charles Wood
Charles Wood

Reputation: 882

How to get and use a transient instance?

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!

Upvotes: 0

Views: 268

Answers (2)

user800014
user800014

Reputation:

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.

Upvotes: 1

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18383

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

Upvotes: 1

Related Questions