1

我有一个对象 Foo,它与 Bar 具有双向一对一关系,另一个与 Baz 具有一对一关系。当我尝试使用 Foo 执行 .load 并且只给它一个 Bar 时,我收到引用完整性异常,抱怨没有 Baz。

真的应该这样吗?在现实世界的环境中,数据库中不可能没有任何匹配的 Baz 对象吗?

我尝试在固定装置负载关闭中手动设置 baz:null ,但我仍然得到同样的结果。附带说明一下,当我只设置属性(例如简单的字符串)时,一切正常。只有当我开始建立关系时。

这是使用 Grails 2.2.4、Fixtures 1.2,并且没有安装 build-test-data 插件。

编辑:我有将 Baz 指定为可为空且唯一的约束。只是为了咯咯笑,我也尝试添加blank约束,但没有运气。

static constraints = {
    baz nullable:true, unique: true, blank: true
}

编辑 2:这是代码的简化版本:

class Foo {
    String someValue1
    String someValue2
    String whatever
    Bar bar
    Baz baz
    static mapping = {
        id composite: ['someValue1', 'someValue2'], generator: 'assigned'
        columns {
            bar([:]) { column name: 'some_other_value' }
            baz ([insertable:false, updateable: false]) {
                column name: 'some_value_1'
                column name: 'some_value_2'
            }
    }

    version: false

    static constraints = {
        //there are no constraints for Bar
        baz nullable:true, unique:true
    }
}

class Bar {
     String someOtherValue
     static hasMany = [foos:Foo]
     static mapping = { 
        id generator:'assigned', name:'someOtherValue'
     }
}

class Baz {
    String someValue1
    String someValue2
    String asdf

    static mapping = {
        id composite: ['some_value_1', 'some_value_2']
        version false
    }
}

class MyTest {
    def fixtureLoader
    @Before
    void setup() {
        fixureLoader.load {
            myBar(Bar, someOtherValue:"shibby")
            myFoo(Foo, someValue1:"test", someValue2:"test2", bar:myBar)
            //i also tried this
            //myFoo(Foo, someValue1:"test", someValue2:"test2", bar:myBar, baz:null)
        }
    }
}

这是例外的一部分:

引起:org.h2.jdbc.JdbcBatchUpdateException:参照完整性约束违规:“FK190E74B120F4F2BC:MYSCHEMA.FOO FOREIGN KEY(SOME_VALUE_1, SOME_VALUE_2) REFERENCES MYSCHEMA.BAZ(SOME_VALUE_1, SOME_VALUE_2)”;SQL 语句:插入 MYSCHEMA.foo (whatever, some_other_value, some_value_2, some_value_1) 值 (?, ?, ?, ?, ?, ?, ?, ?) [23506-164]

编辑:对不起,我之前说错了。Bar 与 Foo 是多对一的关系。

4

1 回答 1

1

您的代码简化版本(hasManyBar 中除外)对我有用,没有任何 FK 异常。尽管如果我对父子映射正确无误,我更希望采用不同的方法来实现真正的一对一双向关系。

下面是我的设置,它在没有 FK 约束异常的情况下工作正常。请注意,我还在评论中提到,假设Foo 有一个 Bar 和一个 Baz,我将如何实现真正的一对一双向。

class Foo implements Serializable{
    String someValue1
    String someValue2
    String whatever

    //True one to one can be achieved by doing as below
    //static hasOne = [bar: Bar, baz: Baz]

    Bar bar
    Baz baz

    static mapping = {
        id composite: ['someValue1', 'someValue2'], generator: 'assigned'
        columns {
            bar([:]) { column name: 'some_other_value' }
            baz ([insertable:false, updateable: false]) {
                column name: 'some_value_1'
                column name: 'some_value_2'
            }
        }
        version: false
    }

    static constraints = {
        //baz nullable:true, unique:true
    }
}

class Bar {
    String someOtherValue

    //True one to one can be achieved by doing as below
    //Below entry makes the relation bi-directional
    //Foo foo

    static mapping = {
        id generator:'assigned', name:'someOtherValue'
        //Optional, added for clarity
        someOtherValue column: 'some_other_value'
    }
}

class Baz implements Serializable{
    String someValue1
    String someValue2
    String asdf

    //True one to one can be achieved by doing as below
    //Below entry makes the relation bi-directional
    //Foo foo

    static mapping = {
        id composite: ['someValue1', 'someValue2']
        //Optional, added for clarity
        someValue1 column: 'some_value_1'
        someValue2 column: 'some_value_2'
        asdf column: 'asdf'

        version false
    }
}

class MyTests extends GroovyTestCase {
    def fixtureLoader

    void setUp() {
        fixtureLoader.load {
            myBar(Bar, someOtherValue:"shibby")
            myFoo(Foo, someValue1:"test", someValue2:"test2", 
                     whatever: "whatever", bar: myBar)
        }
    }

    void testSomething() {
        Foo.all.each{println it.properties}
        Bar.all.each{println it.properties}
    }
}

//Bootstrap Test in Dev mode
new Bar(someOtherValue: 'shibby').save()
new Foo(someValue1:"test", someValue2:"test2", 
        whatever: "whatever", bar: myBar).save(failOnError: true, flush: true)

笔记

  • 上面我使用了您的确切简化代码,但使用hasMany了 Bar 中的关系。
  • 约束Baz是可选的。
  • 夹具按预期工作。
  • 列按预期在 Foo 中创建。
  • logSql显示预期的 DML。
  • 为了见证表格的变化,我还在开发模式下引导了相同的测试数据run-app。我能够看到预期的表结构,其中包含数据,使用dbconsole.
  • 遵循一对一双向的一般方式(称为注释代码),FK 是在子表 [BarBaz] 中创建的,因此您在示例代码中提供的显式映射不会很好。
  • hasMany如果提到关系的拥有方以及拥有 Bar的理由,问题就会更加清楚。
于 2013-08-20T04:02:32.377 回答