Note that there are some explanatory texts on larger screens.

plurals
  1. POError in Grails/GORM: deleted object would be re-saved by cascade
    primarykey
    data
    text
    <p>Looks like this has been asked many many times before, and I have looked into several of those, including the "<a href="http://spring.io/blog/2010/07/02/gorm-gotchas-part-2/" rel="nofollow">GORM Gotchas (Part 2)</a>" by Peter Ledbrook, but still cannot seem to figure it out in my case. I have a <code>user</code> which has many <code>projects</code>, and a <code>project</code> belongs to a <code>user</code>. Then there is a <code>skill</code> which has many <code>projects</code> associated with it, but a <code>project</code> does NOT belong to a <code>skill</code>. When I try to remove a <code>project</code> from a <code>user</code>, I get that error. The domain classes are as follows:</p> <pre> package grailstuts class User { String name static constraints = { name(nullable: true) } static mapping = { projects cascade: "all-delete-orphan" } static hasMany = [ projects: Project ] } </pre> <pre> package grailstuts class Project { String title static constraints = { title(nullable: true) } static belongsTo = [ user: User ] } </pre> <pre> package grailstuts class Skill { String name static constraints = { name(nullable: true) } static mapping = { projects cascade: "all-delete-orphan" } static hasMany = [ projects: Project ] } </pre> <p>When I run the integration test as follows, the part containing <code>foundUser.removeFromProjects(foundProject1)</code> seems to have the issue.</p> <pre> 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 } } </pre> <p>The first <code>given:... when:... then:...</code> block in the above test verifies fine. In the second <code>when:...</code> block when I try to remove the <code>project</code> from the <code>user</code>, that is when the error seem to occur. The error looks something like this ...</p> <pre> | 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 </pre> <p>I am using:</p> <pre> Grails version: 2.3.1 Groovy version: 2.1.8 JVM version: 1.7.0_45 </pre> <p>Have spent quite a bit of time digging for some solution, but no luck yet. Any help would be highly appreciated. Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload