Home > Mobile >  How to intercept an EntityNotFoundException and return fake Entity in JPA/Hibernate
How to intercept an EntityNotFoundException and return fake Entity in JPA/Hibernate

Time:01-30

I have this situation where I have a relationship between two tables but it is not an enforced foreign key relationship, so sometimes the keys set in the first table are referencing missing records in the second, because records have been deleted over the years. Legacy work...

Is there a way to define the n:1 relationship in JPA to not fail if the referenced entity is not present, or to intercept this on hibernate to return a fake entity if it happens?

CodePudding user response:

Check out the @NotFound annotation.

https://docs.jboss.org/hibernate/orm/6.2/userguide/html_single/Hibernate_User_Guide.html#associations-not-found

  • Related