Sub topic 1
The example model: authors and books
Every chapter in this tutorial uses the same small model. An author writes many books, and each book has one author. The model is small enough to read at a glance, but it produces the same query patterns you will see in larger applications. This chapter explains how the two entities are mapped and which loading behavior each mapping gives you.
Prerequisites
This chapter assumes you already have:
- Basic Java and Spring Boot application structure.
- Familiarity with Spring Data JPA repositories.
- Basic SQL, including joins and GROUP BY.
- Basic JPA entity mapping with
@Entity,@Id,@OneToManyand@ManyToOne.
The relationship
The relationship is one-to-many from author to book. In the database, the book table holds a foreign key column that points to author. The author table has no column for its books.
The Author entity
Author is the inverse side of the relationship. It exposes the collection of books but does not own the foreign key.
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "author")
private List<Book> books = new ArrayList<>();
public Long getId() { return id; }
public String getName() { return name; }
public List<Book> getBooks() { return books; }
}The Book entity
Book is the owning side. Its author field maps to the author_id column.
import jakarta.persistence.*;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToOne(fetch = FetchType.LAZY)
private Author author;
public Long getId() { return id; }
public String getTitle() { return title; }
public Author getAuthor() { return author; }
}Each class lives in its own file in a real project.
Why the mapping looks like this
Three details in these classes matter for performance later.
mappedBy = "author"marksAuthor.booksas the inverse side. It tells JPA thatBook.authorowns the foreign key. Without it, JPA would assume a separate join table.@OneToManyis lazy by default. Readingauthor.getBooks()does not load any books. Hibernate gives the author a placeholder collection, and it runs a query only when you first touch that collection.@ManyToOneis eager by default, so the example setsFetchType.LAZYexplicitly. Keeping the default would load the author every time you load a book, whether you need it or not.
| Association | JPA default | This model |
|---|---|---|
| `@OneToMany` (`Author.books`) | Lazy | Lazy (default) |
| `@ManyToOne` (`Book.author`) | Eager | Lazy (explicit) |
The IDENTITY id strategy is the simplest choice for a first example. It has a cost for write performance, which the chapter on JDBC batching covers.
Setting up the repository
A standard Spring Data repository is enough for now.
import org.springframework.data.jpa.repository.JpaRepository;
public interface AuthorRepository extends JpaRepository<Author, Long> {
}Calling findAll() on it returns authors only. The books stay unloaded until your code reads them. That gap between what you loaded and what you later touch is where the N+1 problem comes from, and the next chapters build on it.
Common mistakes with this mapping
- Forgetting
mappedBy. JPA then creates an extra join table (author_books), and the schema no longer matches your intent. - Changing only the inverse side. Adding a book to
author.getBooks()does not setbook.author, so nothing is saved. Set the owning side. - Assuming lazy means free. Lazy loading postpones the query. It does not remove it.
Exercise
Which entity owns the foreign key in this model, and why does Author.books use mappedBy?
Show answer
Book owns the foreign key, through its author field and the author_id column. Author.books uses mappedBy = "author" to declare itself the inverse side. This makes JPA reuse the foreign key on book instead of creating a join table.
Exercise
You load one Book with findById and never call getAuthor(). Does Hibernate query the author table, given the mapping above?
Show answer
No. Book.author is explicitly lazy, so Hibernate stores a proxy and loads the author only when you access its data. With the default eager fetch for @ManyToOne, the author would be loaded with the book.