2

I have a hierarchical structure. Some children are hidden so I have @Where clause:

@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true)
@org.hibernate.annotations.Where(clause = "hidden=false")
private List<Element> children;

But when I want to remove the whole sub-structure (including hidden children) by deleting parent node, hibernate seems to skip hidden children and i get db exception:

org.postgresql.util.PSQLException: ERROR: update or delete on table "xxx" violates foreign key constraint "yyy_yyy_id_fkey" on table "zzz"

Does hibernate have some method of dealing with it?

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • I'd just avoid getting too clever with Hibernate mappings, get rid of the `Where` and move that logic to a DAO. Or split that up into `allChildren` and `visibleChildren` - use the former to manage the entity lifecycle, use the other in client layers. Interface segregation might also be helpful here, although it's a little fiddly to stick to that principle on the entity layer; another option is to always convert between entities and a view model on the layer just under your UI code. (Possibly laborious, but you get rid of lazy-loading and session scope issues.) – millimoose Nov 21 '17 at 15:28

0 Answers0