@OnDelete Policy

Hi Guys
I have two entities

  1. Fruit Set , which contains a list of fruit
  2. fruit, which may or may not belong to a Fruit Set

I’m trying to figure out how to get a constraint working which

  1. If I delete a fruit set it deletes all the nestled fruit
  2. If I try and delete a fruit entity which belongs to a fruit set it denies it

Having a hell of a time.
Also do not want soft delete functionality in either table.

I can get (1) to work with an on delete listener.

But 2 will not work. Even if I set the on delete policy. It just deletes the fruit and removes it from the fruit set.

Is it because I have an entity listener on the fruit entity? Does that override the @OnDelete(DeletePolicy.DENY) policy on the fruit class?

Is it possible to set something in the fruit listener which denies the transaction if the fruit belongs to a fruit set? I cannot see any methods to override or anything…

Hi,

(1) can be reached without entity listener. Just add @OnDeleteInverse(DeletePolicy.CASCADE) to the Fruit entity on the fruitSet attribute.

For (2) you need to create an entity listener and check if there are any fruit sets which contain given fruit.

I’ve prepared a sample project on GitHub. Take a look at data model and FruitEntityListener.

Regards,
Gleb