1

我正在使用 Grails 2.4.4。我想用内存数据库测试单元测试类的持久性。我有一个与 Child 有 oneToMany 关系的 Parent 类。Child 由 Parent 拥有,并且具有涉及父级的复合键。当我尝试删除 Parent 内的集合中的一个 Children 时,如果我刷新,我会收到一个错误,如果我省略 'flush: true' 参数,则不会触发删除。

class Parent implements Serializable {
    String name

    static hasMany = [children : Child]

    static mapping = { 
        children cascade: 'all-delete-orphan'
    }
}

class OtherParent implements Serializable {
    String name
}

class Child implements Serializable {
    String name

    static belongsTo = [ owner : Parent, secondOwner : OtherParent]

    static mapping = {        
        id composite : ['owner', 'secondOwner']
    }

}

我想测试像这样注释的单元测试类中的关系

@Domain([Parent, OtherParent, Child])
@TestMixin(HibernateTestMixin)
class ChildSpec extends Specification {

        def "Parents and Children can be created, saved and deleted"() {
            given: "we have a clean database at the start"
                Parent.count() == 0
                OtherParent.count() == 0
                Child.count() == 0

            and:
                Parent a = new Parent()
                a.name = "Parent"
                OtherParent secondParent = new OtherParent ()
                secondParent.name = 'Second Parent'         
                Child b = new Child()
                b.name = "Child"
                b.otherOwner = secondParent
                a.addToChildren(b)

            when: "we save Parent"
                secondParent.save(flush: true, failOnError: true)
                a.save(flush: true, failOnError: true)

            then: "Parent saves and Child is saved too"
                Parent.count() == 1
                Child.count() == 1
                def savedA = Parent.findByName("Parent")
                savedA.name == "Parent"
                savedA.children.size() == 1
                def savedB = savedA.children.getAt(0)
                savedB.name == "Child"
                def foundB = Child.findByName("Child")
                foundB.name == "Child"

            when: "we remove Child from Parent, we can still save Parent"
                savedA.removeFromChildren(savedB)
                savedB.delete(flush: true)
                savedA.save(failOnError: true, flush: true)

            then: "we've got an Parent with no Bs and no exception is thrown"
                notThrown(Exception)
                Child.count() == 0
        }
}

但是抛出异常

Expected no exception of type 'java.lang.Exception' to be thrown, but got it nevertheless
    at spock.lang.Specification.notThrown(Specification.java:106)
    at eu.europa.ec.comp.redda.test.ParentSpec.Parents and Chilren can be created, saved and deleted(ParentSpec.groovy:56)
Caused by: org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Object of class [eu.europa.ec.comp.redda.test.Child] with identifier [eu.europa.ec.comp.redda.test.Child : (unsaved)]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [eu.europa.ec.comp.redda.test.Child#eu.europa.ec.comp.redda.test.Child : (unsaved)]
    at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:200)
    at org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate.convertHibernateAccessException(GrailsHibernateTemplate.java:593)
    at org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:183)
    at org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:123)
    at org.codehaus.groovy.grails.orm.hibernate.InstanceApiHelper.delete(InstanceApiHelper.java:36)
    at org.codehaus.groovy.grails.orm.hibernate.HibernateGormInstanceApi.delete(HibernateGormInstanceApi.groovy:228)
    at eu.europa.ec.comp.redda.test.ParentSpec.Parents and Chilren can be created, saved and deleted(ParentSpec.groovy:52)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [eu.europa.ec.comp.redda.test.Child#eu.europa.ec.comp.redda.test.Child : (unsaved)]
    at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2541)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3403)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
    at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.codehaus.groovy.grails.orm.hibernate.InstanceApiHelper$1.doInHibernate(InstanceApiHelper.java:40)
    at org.codehaus.groovy.grails.orm.hibernate.InstanceApiHelper$1.doInHibernate(InstanceApiHelper.java:36)
    at org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:179)
    ... 4 more
4

1 回答 1

0

您的单元测试需要为您的测试设置一些数据。如果你想测试你是否可以创建和保存你的对象,那么试试这个(你还缺少nameA 域类的属性,我们也假设 B 类也有一个属性):

def "As and Bs can be created, saved and deleted"() {
    expect: "we have a clean database at the start"
        A.count() == 0
        B.count() == 0

    given:
        A a = new A()
        a.name = "A"
        B b = new B()
        b.name = "B"
        a.addToBs(b)

    when: "we save A"
        a.save(flush: true, failOnError: true)

    then: "A saves and B is saved too"
        A.count() == 1
        B.count() == 1
        def savedA = A.findByName("A")
        savedA.name == "A"
        savedA.bs.size() == 1
        def savedB = savedA.bs.getAt(0)
        savedB.name == "B"
        def foundB = B.findByName("B")
        foundB.name == "B"

    when: "we remove B from A, we can still save A"
        savedA.removeFromBs(savedB)
        savedB.delete(flush: true)
        savedA.save(flush: true)

    then: "we've got an A with no Bs and no exception is thrown"
        notThrown(Exception)
        savedA.bs.count() == 0
}

编辑以反映问题的变化:

您的域模型意味着 Child 必须同时属于 Parent 和 OtherParent;这不是非此即彼的关系。你确定这是你想要的吗?

如果是你想要的,我还是不明白你为什么要一个复合id?此外,您还没有在@Domain注释中包含 OtherParent 。如果您删除复合 id 映射,此处的代码将起作用。

于 2015-03-03T10:46:34.143 回答