FetchType: Lazy/Eager loading for Hibernate & JPA
|

FetchType: Lazy/Eager loading for Hibernate & JPA

Choosing the right FetchType is one of the most important decisions when defining your entity mapping. It specifies when your JPA implementation, e.g., Hibernate, fetches associated entities from the database. You can choose between EAGER and LAZY loading. The first one fetches an association immediately, and the other only when you use it. I explain…

Hibernate Performance Tuning – 2024 Edition
| |

Hibernate Performance Tuning – 2024 Edition

Based on most discussions online and at conferences, there seem to be 2 kinds of projects that use Hibernate for their persistence layer: So, what’s the difference between these projects? Are the projects in the 2nd group more complex or have higher performance requirements? No, based on my consulting projects, that’s not the case. On…

How to persist additional attributes for an association with JPA and Hibernate
|

How to persist additional attributes for an association with JPA and Hibernate

JPA and Hibernate allow you to define associations between entities with just a few annotations, and you don’t have to care about the underlying table model in the database. Even join tables for many-to-many associations are hidden behind a @JoinTable annotation, and you don’t need to model the additional table as an entity. That changes…

Hibernate’s @NotFound Annotation – How to use it and a better alternative

Hibernate’s @NotFound Annotation – How to use it and a better alternative

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…

Polymorphic association mappings of independent classes

Polymorphic association mappings of independent classes

JPA and Hibernate make it very easy to model associations between entities. You can model associations between 2 concrete classes or model a polymorphic association to an inheritance hierarchy. These mappings are more than sufficient for almost all of your association mappings. But sometimes, you might want to model a polymorphic association to independent entity…