问题
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