Question

I am new to grails and still trying to wrap my head around the mapping of objects. In our project we have three classes that are causing some problems Attendee, Vendor, and Person An attendee has one person and a vendor has many persons so we went with the following setup:

class Person{
  String firstName
  //Other details...
}

class Attendee {
  Person person
}

class Vendor{
  static hasMany = [
    person:person
  ]
}

So the objects are being hydrated via a web form and I can confirm that the person details are being hydrated from a log statement. However we get the following error:

Message ORA-01400: cannot insert NULL into ("EIGHT_STATES_USER"."ATTENDEE"."PERSON_ID")

so we added static belongsTo = [attendee: Attendee, vendor: Vendor] to our Person based on a stackoverflow we read. But then when we tried to save the Attendee it wanted to create a Vendor.

Not sure where to go from here.

Était-ce utile?

La solution

Try adding a mapping to your Attendee object:

Person person

static mapping = {
  person cascade: "all"
}

More information about the custom mapping can be found here: http://grails.org/doc/2.3.x/guide/GORM.html#customCascadeBehaviour

Autres conseils

The way you currently have it defined, you need to save the Person object first and then add it to the Attendee and save. You don't need the belongsTo in Person.

class Person {
  String firstName
  //Other details...
}

class Attendee {
  Person person
}

class Vendor {
  static hasMany = [
    people:Person
  ]
}

def person = new Person(params)
if (person.save(flush:true)) {
  def attendee = new Attendee(params)
  attendee.person = person 
  attendee.save(flush:true)
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top