Announcement Announcement Module
Collapse
No announcement yet.
Deleting an instance of an object with a MANY_TO_MANY relationship without cascading? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting an instance of an object with a MANY_TO_MANY relationship without cascading?

    I've got two classes (Students and Courses) which have a many-to-many-relationship.
    Instances of them can exist without the other one.
    Therefore I removed the cascade-attribute in the JPA-annotation: @ManyToMany(cascade = CascadeType.ALL)

    Once I try to delete a Student I get this error-message.
    What am I doing wrong?

    Code:
    org.hibernate.exception.ConstraintViolationException: could not delete: [com.Student#4]; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not delete: [com.Student#4]
    This is the Hibernate-output:
    Code:
    Hibernate: delete from student where id=? and version=?
    2011-09-19 15:25:10,317 [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot delete or update a parent row: a foreign key constraint fails (`database`.`student_course`, CONSTRAINT `FKF8A06F72970A31AF` FOREIGN KEY (`students`) REFERENCES `students` (`id`))
    These are the relevant parts of both classes:

    Code:
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Student{
    
        @ManyToMany
        private Set<Course> courses= new HashSet<Course>();
    }
    
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Course {
    
        @ManyToMany(mappedBy = "courses")
        private Set<Student> students= new HashSet<Student>();
    }

  • #2
    In that case you need to make sure you first remove the Student reference from the Course.students collection before removing the actual record.

    Comment


    • #3
      How do I delete the reference in Roo?

      Comment

      Working...
      X