Entity relations with Hibernate 4

This entry is part 4 of 13 in the series Hibernate Tutorial

Last time I introduced annotations instead XML configuration. Now I will dig deeper and show how you can create entity relations and map them to the database.

If you look at the Book entity in the example you might think: “Why store the authors of a book as a String?” and you are right. This makes querying for books by authors almost impossible — or at least not as performant as it could be. And a typo between two entries can make it even worse.

In this article I will take the example further and extract the String of authors into another entity.

The Author entity

Let’s start with a new entity holding Author information. For simplicity it will contain only the name of the author and the list of Books an author created.

As you can see, there is a new annotation: @ManyToMany. This tells Hibernate that an Author entity maps to other Book entities. The mappedBy attribute is used to tell Hibernate which entity is the owner side of the relation. This is used mostly in one-to-many or many-to-one relations.

Adding authors to the Book entity

Now let’s change the author String to a list of Authors. It will look quite the same than in the Author entity:

Here I did not add any parameter to the @ManyToMany annotation because the Author is the owner side.

Relation types

Of course the many-to-many relation is not the only one available. You can choose one-to-one, one-to-many or many-to-one too if it fits your needs.

One-to-one relations

This relation type contains for one entity only one of the referenced type — and the same goes for the other entity. In this case you can choose which entity you use to store the reference in. An example would be the relation between Books and ISBN numbers if I would extract the ISBN as a separate entity with some other properties: one book has one ISBN and one ISBN refers to exactly to one book.

One-to-many and many-to-one relations

In this case one entity has many references in other entities. This would be the case if one book could have only one author. In this case the referencing ID would be stored in the BOOKS table because one book references to its author. We use one-to-many in the Author entity and many-to-one in the Book entity.

Many-to-many relations

As you could see in the first example, this relation is a bit more complex. Here we need a separate table to contain the references between books and authors. This is the so called join-table. This table is maintained by Hibernate automatically but you can tell how the table should be named. If you do not choose a name, Hibernate takes the entity names and separates them with an underscore, the owner site-entity is the later name. In this example the join-table is named: BOOKS_AUTHORS.

Changing the main method

Because the entities are changed I have to change the main method of the Main class too.

As you can see, I use some lambdas to dynamically create the authors. The interesting part is the last statement, the forEach block: I add the book to the list of books of the current author, and then I add the current author to the book’s authors list. This reference is needed to find the books and authors together later in the database (if you query manually or load the dataset with Hibernate).

The result is now a bit different from the last time:

If I leave away the statement book.getAuthors().add(author); then the result does not contain the authors’ names:


As you could see, there are a bunch of options how you can map your entity relations to the normalized database. Next time I will show you how you can map entity inheritance to your database.

Code Download

You can download chapter specific code from Github Here.

Series Navigation<< Hibernate 4 annotations ConfigurationEntity inheritance models in Hibernate 4 >>

Leave A Comment

Your email address will not be published. Required fields are marked *