Reputation: 3595
I am working on a project and I have two domains.
class Author {
Book book
String name
}
class Book {
Author author
String title
}
I have saved an instance of domain Author in database and in a service I do something like this:
def authorInstance = Author.getById(1)
def bookInstance = new Book(author:authorInstance, title: "Foo")
But i do not save the bookInstance rather, I use it for couple of more processes. This gives me org.hibernate.TransientObjectException. I also tried to do something like:
def authorInstance = Author.getById(1)
def aI = authorInstance
def bookInstance = new Book(aI, title: "Foo")
But in this case too, I get the same error. I am working in this way because I am working in legacy code, so I cannot change much. Is there a work around for this ?
Upvotes: 0
Views: 403
Reputation: 384
By the way, there is another issue with your possible replacement code. Instead of:
def bookInstance = new Book(aI, title: "Foo")
you would need
def bookInstance = new Book(author:aI, title: "Foo")
Book has an in-memory constructor declared which takes a Map object and "author" and "title" are keys into that Map. This constructure then uses the map to initialize class members.
Upvotes: 0
Reputation: 13475
You're apparently changeing some field of authorInstance you set. That's what TransientObjectException
says: "object references an unsaved transient instance"
. Please do read and do quote the error messages.
Save the Author
before saving a book. Or do not modify it.
An you probably would like to use hasMany and belongsTo.
Upvotes: 1