【问题标题】:Grails mapping objectsGrails 映射对象
【发布时间】:2014-04-01 14:18:51
【问题描述】:

我是 grails 的新手,但我仍在尝试围绕对象映射进行思考。在我们的项目中,我们有三个类会导致一些问题 AttendeeVendorPerson 参加者有一个人,而供应商有很多人,所以我们采用以下设置:

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

class Attendee {
  Person person
}

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

因此,这些对象正在通过网络表单进行补充,我可以确认 person 的详细信息正在从日志语句中补充。但是我们得到以下错误:

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

所以我们根据我们阅读的堆栈溢出将static belongsTo = [attendee: Attendee, vendor: Vendor] 添加到我们的Person。但是当我们尝试保存Attendee 时,它想创建一个Vendor

不知道从这里去哪里。

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    尝试将映射添加到您的参加者对象:

    Person person
    
    static mapping = {
      person cascade: "all"
    }
    

    关于自定义映射的更多信息可以在这里找到:http://grails.org/doc/2.3.x/guide/GORM.html#customCascadeBehaviour

    【讨论】:

      【解决方案2】:

      按照您当前定义的方式,您需要先保存 Person 对象,然后将其添加到参加者并保存。个人不需要 belongsTo。

      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)
      }
      

      【讨论】:

      • 感谢您快速给出确实有效的答案,但是......我们试图避免以这种方式保存它。 Risu 在她的回答中所说的话奏效了。
      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 2019-09-19
      • 1970-01-01
      • 2011-09-13
      • 2013-08-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      相关资源
      最近更新 更多