【问题标题】:relation 1 a 1 error insert data关系 1 a 1 错误插入数据
【发布时间】:2016-01-28 01:40:20
【问题描述】:

我按照自己的方式建立了 1-1 域模型

package relation1a1

class Person {
    Home home
    String name
    String aPaterno
    String aMaterno
}

package relation1a1

class Home {

    static belongsTo = [person: Person]

    String cP
    String street
}

当我向 person 表插入数据时,要求我在第一个工作表中插入数据,当我尝试将数据插入主表时,要求我将数据插入 person 表中。

我试图做的是一种消除工作的1-1人关系。

【问题讨论】:

  • first jobwork表是什么意思?
  • 感谢您的回答,我无法在任何两个表中插入数据
  • 展示如何插入PersonHome 会很有帮助。

标签: grails grails-orm crud domain-model


【解决方案1】:

更改类域:

package relation1a1

class Person {
    Home home
    String name
    String aPaterno
    String aMaterno
    static constraints = {
        home nullable: true //now you can save Person instance with home=null
    }
}

比:

def person = new Person(name: "name", aPaterno: "qwe", aMaterno: "rty")
person.save(flush: true)

def home = new Home(cP: "asd", street: "fgh", person: person)
home.save(flush: true)

person.home = home
person.save()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2021-02-20
    相关资源
    最近更新 更多