[Grails] 【急】关于Grails级联删除

cuttlefish 2008-11-17
单向One-to-One 无法级联删除

Domain Class:
class Parent {
} 


class Child {
    static belongsTo = [parent:Parent]
}


class ParentTests extends GroovyTestCase {

    void testSomething() {
        def p = new Parent().save()

        assertEquals(1,Parent.count())

        def c = new Child(parent:p).save()
        assertEquals(1,Child.count())

        p.delete()
        assertEquals(0,Parent.count())
        assertEquals(0,Child.count())
    
    }
}


错误:

org.hibernate.exception.ConstraintViolationException: could not delete: [Parent#1]
...
Caused by: java.sql.SQLException: Integrity constraint violation FK5A3F51C976A59A table: CHILD in statement [delete from parent where id=? and version=?]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
令狐虫 2008-11-17
改为双向的吧:
class Parent{
    Child child
}
class Child {   
    static belongsTo = [parent:Parent]   
}
cuttlefish 2008-11-17
改成双向后可以删除,但是没有级联删除。。。

junit.framework.AssertionFailedError: expected:<0> but was:<1>
令狐虫 2008-11-17
p.delete(flush:true)
试试
cuttlefish 2008-11-17
还是一样……不行……
令狐虫 2008-11-17
应该行呀
我来试试看
cuttlefish 2008-11-17
好的谢谢啦
令狐虫 2008-11-17
测试用例有问题
我改一下就通过了:
class ParentTests extends GroovyTestCase {

    void testSomething() {
        def c = new Child()
        def p = new Parent(child:c)
        p.save(flush:true)
        assertEquals(1,Parent.count())
        assertEquals(1,Child.count())

        p.delete(flush:true)
        assertEquals(0,Parent.count())
        assertEquals(0,Child.count())
    }
}
Global site tag (gtag.js) - Google Analytics