0

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.

1 Answers1

0

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/

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
ProgrammerDan
  • 871
  • 7
  • 17
  • I discovered Vlad's blog just a few minutes before you posted your answer. I'm trying to use Hibernate's Cascading rather than javax.persistence. I actually want to be able to SAVE_UPDATE too; so I included a bunch of different cascade types-except for remove. I'm still running into this problem where table b's record gets deleted when i don't want it to be. I'm beginning to wonder if perhaps I have another cascading relationship thats making that record be deleted. Thanks for the help. – shiningcartoonist Nov 11 '15 at 18:29
  • Sure thing, good luck. It can often be helpful to turn on hibernate's query logging -- http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values -- as well; it can help pinpoint the relationships you might be missing that Hibernate is traversing. – ProgrammerDan Nov 11 '15 at 18:31
  • @shiningcartoonist Had a thought for something you might try, if you haven't already -- and depending on the size of your project -- you could simply annotate all your joins with ```cascade = CascadeType.NONE```, then slowly add specific cascade types back in while testing the results. It's a good practice to carefully control the kinds of cascading and the way joins are traversed, so the exercise may yield additional performance benefits. – ProgrammerDan Nov 12 '15 at 14:47