Hibernate @NotFound - How to handle broken foreign key reference

preview_player
Показать описание
Some table models don’t enforce their foreign key references by a foreign key constraint. This is a bad practice that often leads to foreign key references that point to non-existing records. When Hibernate tries to resolve such a broken reference, it throws an EntityNotFoundException. You should, therefore, define a foreign key constraint for every foreign key reference.

By default, Hibernate throws an exception if it tries to resolve a broken foreign key reference. The best way to fix this is, of course, to clean up your database and fix your foreign key references. But if that’s not an option, you need to decide if you:
want to handle an EntityNotFoundException every time you call the getter method of a potentially broken association or
use Hibernate’s @NotFound annotation to tell Hibernate to fetch a potentially broken association and ignore it or throw a FetchNotFoundException when instantiating your entity object.

Links mentioned in the video:

Like my channel? Subscribe!

Want to connect with me?
Рекомендации по теме
welcome to shbcf.ru