I have a few domain classes that are setup as such:
Person
- Name
OrganizationPerson extends Person
- Role
- Organization
User
- Person
In Bootstrap.groovy, I'm creating a sample user:
def person = OrganizationPeople.findOrSaveWhere(name:'admin', emailAddress: 'steven@admin.com')
person.save(flush:true)
def user = User.findOrSaveWhere(person: person, organization: organization,
username: 'admin', password: 'asdfasdf')
However, when the code ends up hitting the User.findOrSaveWhere
I get an error:
Error initializing the application: object references an unsaved
transient instance - save the transient instance before flushing:
com.eventrosity.Person; nested exception
However, before the OrganizationPerson
inherited Person
, this wasn't an issue. The person.save(flush:true)
saved the item.
Is this an issue in GORM? Is this an issue that I need to be aware of in the context of domain objects and inheritance?
This is probably just a standard validation problem - the OrganizationPeople
instance save failed, so you can't proceed with saving the User
. When you call save()
(or something like findOrSaveWhere
that calls it for you) there won't be an obvious error or an exception. But it's easy to check if the save succeeded:
def person = OrganizationPeople.findOrSaveWhere(...)
if (person.hasErrors()) {
// handle the error
}
else {
def user = User.findOrSaveWhere(...)
if (user.hasErrors()) {
// handle the error
}
}
Note that the explicit person.save()
call is redundant since findOrSaveWhere
already called save()
for you.