Grails / GORM中的错误:被删除的对象将被重置

看起来这个问题之前已经有很多次被问过了,我已经研究了其中的几个,其中包括Peter Ledbrook的“GORM Gotchas(第2部分)”,但似乎还没有弄清楚我的情况。 我有一个拥有很多projectsuser ,并且一个project属于一个user 。 然后有一个skill其中有许多projects与它相关联,但project不属于skill 。 当我尝试从user删除一个project ,我遇到了这个错误。 域类如下所示:


    package grailstuts

    class User {
        String name

        static constraints = { name(nullable: true) }

        static mapping = { projects cascade: "all-delete-orphan" }

        static hasMany = [ projects: Project ]
    }


    package grailstuts

    class Project {
        String title

        static constraints = { title(nullable: true) }

        static belongsTo = [ user: User ]
    }


    package grailstuts

    class Skill {
        String name

        static constraints = { name(nullable: true) }

        static mapping = { projects cascade: "all-delete-orphan" }

        static hasMany = [ projects: Project ]
    }

当我如下运行集成测试时,包含foundUser.removeFromProjects(foundProject1)的部分似乎有问题。


    package grailstuts

    import spock.lang.*

    class SkillIntegrationSpec extends Specification {
        void "adding and deleting projects"() {
            given: "A user, and projects added to the user"
            def user = new User().save(failOnError: true)
            def project1 = new Project(title: "java, groovy")
            def project2 = new Project(title: "java, scala")
            user.addToProjects(project1)
            user.addToProjects(project2)

            when: "Projects are also added to skill entry"
            def java = new Skill(name: 'java').save(failOnError: true)
            java.addToProjects(project1)
            java.addToProjects(project2)
            def groovy = new Skill(name: 'groovy').save(failOnError: true)
            groovy.addToProjects(project1)
            def scala = new Skill(name: 'scala').save(failOnError: true)
            scala.addToProjects(project2)

            then: "Each skill entry has its corresponding projects"
            Skill.findByName("java").projects.size() == 2
            Skill.findByName("groovy").projects.size() == 1
            Skill.findByName("scala").projects.size() == 1

            when: "Some projects are deleted from the database"
            def foundUser = User.get(user.id)
            def foundProject1 = Project.findByTitle("java, groovy")
            foundUser.removeFromProjects(foundProject1)

            then: "The deleted projects are also reflected in the skill entry"
            Skill.findByName("java").projects.size() == 1
            Skill.findByName("groovy").projects.size() == 0
            Skill.findByName("scala").projects.size() == 1
        }
    }

第一个given:... when:... then:...在上面的测试中,block验证没问题。 在第二个when:...当我尝试从user删除project时,就是在错误出现时。 错误看起来像这样...


    | Running 1 integration test... 1 of 1
    | Failure:  adding and deleting projects(grailstuts.SkillIntegrationSpec)
    |  org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade (remove deleted object from associations): [grailstuts.Project#2]; nested exception is org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [grailstuts.Project#2]
        at org.grails.datastore.gorm.GormStaticApi.methodMissing_closure2(GormStaticApi.groovy:102)
        at grailstuts.SkillIntegrationSpec.adding and deleting projects(SkillIntegrationSpec.groovy:34)
    Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [grailstuts.Project#2]
        ... 2 more
    | Completed 1 integration test, 1 failed in 0m 0s
    | Tests FAILED  - view reports in C:Grailsgrailstutstargettest-reports

我在用:


    Grails version: 2.3.1
    Groovy version: 2.1.8
    JVM version: 1.7.0_45

花了相当多的时间挖掘一些解决方案,但没有运气。 任何帮助将不胜感激。 谢谢!


all-delete-orphan - 仅适用于一对多关联,并指示当从关联中删除某个子项时,它应该被自动删除 。 父母时,孩子也被删除。 因此,当您调用foundUser.removeFromProjects(foundProject1)您正在尝试删除foundProject1对象,但无法从DB中删除foundProject1 Skill实体仍然会引用此项目。 如何解决它:

  • 第一种方法:从User移除projects cascade: "all-delete-orphan"以防止在调用user.removeFromProjects(...)时删除项目
  • 第二种方法:用save项目调用skill.removeFromProjects(...) ,这样项目就会被删除
  • 链接地址: http://www.djcxy.com/p/24335.html

    上一篇: Error in Grails/GORM: deleted object would be re

    下一篇: Grails (GORM) Many