Sub topic 4
Modeling the resource as a JPA entity
This chapter assumes you know Java classes and annotations, basic SQL, and have a Spring Boot project with the JPA starter and a configured database. It covers how one Java class maps to one table.
What an entity is
An entity is a Java class that JPA (the Java Persistence API) maps to a database table. Each instance is one row. Each field is one column. Hibernate, the JPA implementation in Spring Boot, reads the annotations and generates the SQL for you.
The Book entity
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
protected Book() {
}
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public Long getId() { return id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
}Each piece does a specific job:
@Entitymarks the class as persistent. Without it, Hibernate ignores the class.@Idmarks the primary key field.@GeneratedValue(strategy = GenerationType.IDENTITY)tells the database to assign the key. On PostgreSQL this uses an identity column. Hibernate reads the generated value back after the insert.- The protected no-argument constructor is required. Hibernate uses it to create instances when it loads rows. Making it
protectedstops application code from creating a half-builtBook. - The public constructor is the one your code uses. It takes the values a new book needs and leaves
idout. - There is no
setId. The database owns the key, so the application should not change it.
The id is a Long rather than a long on purpose. A new object has not been saved yet, so its id is null. A primitive long would show 0, which looks like a real key.
Import namespace
Spring Boot 3 and later use jakarta.persistence. Older tutorials show javax.persistence. If you mix them, the annotations compile but Hibernate silently ignores them, or the build fails with missing classes.
Table and column names
Spring Boot's default naming strategy turns Book into a table named book. A field like publishedYear would become the column published_year. If your table has different names, add @Table(name = "...") or @Column(name = "...").
The configuration from the previous chapter sets spring.jpa.hibernate.ddl-auto=validate. Hibernate then checks that the schema matches your entities and fails at startup if it does not. It never creates tables for you. With PostgreSQL you must create the table yourself:
CREATE TABLE book (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title VARCHAR(255),
author VARCHAR(255)
);Common errors
| Symptom | Likely cause |
|---|---|
| `Not a managed type: class Book` | The class lacks `@Entity`, or sits outside the package of the `@SpringBootApplication` class. |
| `No default constructor for entity` | The no-argument constructor is missing. |
| `Schema-validation: missing table [book]` | `ddl-auto=validate` is set but the table does not exist. |
| `Schema-validation: wrong column type` | The Java type and the column type disagree, for example `String` against an integer column. |
| `id` stays `null` after `save` | You read the id from your original object. Use the object `save` returns. |
Exercise
Why does the Book entity have a protected no-argument constructor and a separate public constructor with title and author?
Show answer
Hibernate needs a no-argument constructor to create instances when it loads rows, and protected is enough because Hibernate can reach it. Application code uses the public constructor, which takes only the values a new book needs. It leaves out the id because the database generates it.
Exercise
You add a field private int pages; to Book and restart with ddl-auto=validate. Startup fails. What went wrong and how do you fix it?
Show answer
Hibernate expects a pages column in the book table, and the table does not have one. Add the column to the database, for example with ALTER TABLE book ADD COLUMN pages INTEGER NOT NULL DEFAULT 0;, and restart. Validation then passes.