之前看起来好像很多次,我已经看过其中的几个,包括Peter Ledbrook的“GORM Gotchas (Part 2)”,但在我的案例中似乎仍然无法解决。我的user
有projects
个project
,而user
属于skill
。然后有一个projects
与project
相关联,但skill
不属于project
。当我尝试从user
中移除foundUser.removeFromProjects(foundProject1)
时,我收到了该错误。域类如下:
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 ] }
当我按如下方式运行集成测试时,包含given:... when:... then:...
的部分似乎有问题。
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 } }
上述测试中的第一个when:...
块验证正常。在我尝试从project
中删除user
时,在第二个{{1}}块中,即错误似乎发生的时间。错误看起来像这样......
| 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:\Grails\grailstuts\target\test-reports
我正在使用:
Grails version: 2.3.1 Groovy version: 2.1.8 JVM version: 1.7.0_45
花了不少时间挖掘一些解决方案,但还没有运气。任何帮助将受到高度赞赏。谢谢!
答案 0 :(得分:4)
all-delete-orphan
- 仅适用于一对多关联和
表明
从关联中删除子项后,应自动删除
。孩子们也被删除了
父母是。因此,当您调用foundUser.removeFromProjects(foundProject1)
时,您尝试删除foundProject1
对象,但无法从数据库中删除因为Skill
实体仍然引用此项目。如何解决它:
projects cascade: "all-delete-orphan"
移除User
,以防止在致电user.removeFromProjects(...)
skill.removeFromProjects(...)
,以便删除项目