Search code examples
javahibernatehibernate-cascadejpa

Ask Hibernate to not perform the cascade when using a join table?


When I delete a record from table r, it in turn deletes a record from a linking table b because r has a Many-To-Many relationship with b and I am using Hibernate Join Table to associate them. Here is where my problem comes: I have a view that relies on that linking table, b, and the result of deleting r causes a delete from the view but now that view record is no longer there. I get a Stale State exception because of this.

Can I ask Hibernate to ignore cascade deletes? I have tried evicting the b table records before trying to delete, but that doesn't appear to work.


Solution

  • Bit rusty on this, but I believe you can control cascade behavior in your join declaration annotation, like so:

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private List<OtherData> dontDeleteMe;
    

    Which would cascade persist and merge operations, but not updates or deletes.

    Check this page out for more details:

    http://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/