Associations with JPA and Hibernate

The handling of associations between entities and their underlying database records is one of the key strengths of JPA. It hides most of the technical complexity and enables you to concentrate on the business logic you want to implement.

At the same time, it’s often the reason for performance problems. A seemingly small misconfiguration in your mapping definition or a missing optimization when loading your entities can cause lots of unexpected SQL statements.

You can easily avoid these issues, if you have a solid understanding of the mapping definitions, know when and why Hibernate executes additional queries and follow a few best practices. 

Association Mappings

The definition of a basic entity mapping is simple. It only requires an entity attribute and 1-2 annotations. On the right, you can see an example mapping of a standard many-to-one association mapping.

Based on this mapping, your persistence provider, e.g., Hibernate, knows the database tables and the foreign columns that represent the association in your table model. It then generates all the required SQL statements to fetch the association from the database and to persist all changes.

I explain all of these mappings in great detail in the following article. If you don’t have a lot of experience with the mapping of associations, you should start there.

Advanced Association Mappings

OK, let’s take it one step further. Based on the basic mappings described in the previous article, you can model all kinds of associations. You can model self-referencing associations, persist additional attributes for each association record, define associations between more than 2 entities and much more.

Modeling self-referencing associations with Hibernate

When you model a hierarchical data structure, you often have to use self-referencing associations. Both ends of these associations are of the same type. In other words, the entity object on which the association is defined and the one the association references are of the same type. In your database, you model that using a…

Ternary Associations – Modelling Associations between 3 Entities

An association between 3 entities is called a ternary association. A typical example is an association between an employee, the project they are working on, and their role in that project. If the role is a complex object, you might decide to model this as 3 entity classes. The association between them is the interesting…

Improve Usability and Avoid Performance Issues

If you want to use these different kinds of associations as efficient as possible, you also need to be familiar with JPA’s FetchTypes, the side-effects of cascade operations, and Hibernate’s handling of different Collection types.

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…

Association Fetching

The way you fetch your associations often makes the difference between a blazingly fast application and huge performance problems. But don’t worry, doing it right is much easier as you might think. As long as you use the FetchType.LAZY and initialize all required associations when you fetch your entity, you should avoid at least 90% of all performance problems.

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…

Your 2 best options to fix Hibernate’s MultipleBagFetchException

You probably learned that you should use FetchType.LAZY for all of your associations. It ensures that Hibernate initializes an association when you use it and doesn’t spend any time getting data you don’t need. Unfortunately, this introduces a new issue. You now need to use a JOIN FETCH clause or an EntityGraph to fetch the…

Best Practices

The easiest way to ensure that you applied the most important parts of all the things you learned in the previously mentioned articles is to follow common best practices. So, make sure to keep the following articles at hand and to check them regularly.

Best Practices for Many-to-Many Associations with Hibernate and JPA

Many-to-Many associations are one of the most commonly used associations with JPA and Hibernate. You can find lots of examples for them in the real world, and you can map them with JPA and Hibernate as a uni- or bidirectional association in your domain model. But you probably also know that these mappings provide several…

Best Practices for Many-To-One and One-To-Many Association Mappings

When you model your database, you will most likely define several many-to-one or one-to-many associations. And it’s, of course, the same when you model your entities. It’s quite easy to do that with JPA and Hibernate. You just need an attribute that represents the association and annotate it with a @ManyToOne or @OneToMany association. But…