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 details 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.

Mapping Collections with Hibernate and JPA

JPA and Hibernate provide 3 main options to map a Collection. If it’s a Collection of other entities, you can model it as a to-many

Read 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

Read More

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,

Read More

How to map an association as a java.util.Map

The java.util.List<SomeEntityClass> is the most common representation of a to-many association with JPA and Hibernate. But is it also the one that you want to

Read More

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.

How to Choose the Most Efficient Data Type for To-Many Associations – Bag vs. List vs. Set

Which data type should you use to map a to-many association with Hibernate? Is it better to use a Set or a List? That’s a

Read More

Why you should avoid CascadeType.REMOVE for to-many associations and what to do instead

The CascadeTypes REMOVE and ALL, which includes REMOVE, provide a comfortable option to remove an entity together with all its child entities. But it creates

Read More

Entity Mappings: Introduction to JPA FetchTypes

The FetchType defines when Hibernate gets the related entities from the database, and it is one of the crucial elements for a fast persistence tier. In

Read More

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.

How to Initialize Entity Associations with Spring Data JPA

When we’re talking about performance optimizations for Spring Data JPA, the handling of associations is always an important topic. Because Spring Data JPA is based

Read More

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

Read More

The best way to fetch an association defined by a subclass

EntityGraphs and JOIN FETCH clauses provide an easy and efficient way to fetch an entity and initialize its associations. But if you try to use

Read More

LazyInitializationException – What it is and the best way to fix it

The LazyInitializationException is one of the most common exceptions when working with Hibernate. There are a few easy ways to fix it. But unfortunately, you

Read More

Entity Mappings: Introduction to JPA FetchTypes

The FetchType defines when Hibernate gets the related entities from the database, and it is one of the crucial elements for a fast persistence tier. In

Read More

5 ways to initialize lazy associations and when to use them

Lazy loading of associations between entities is a well established best practice in JPA. Its main goal is to retrieve only the requested entities from

Read More

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

Read More

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

Read More