Sub topic 3
Mapping a Class to a Table with @Entity
This chapter assumes you know core Java, basic SQL (tables, primary keys, foreign keys) and Spring Boot basics. You will map a class to a table and control how its fields become columns.
What @Entity does
An entity is a Java class that JPA maps to a database table. Each instance represents one row. Each persistent field represents one column.
A class needs four things to be a valid entity:
- The
@Entityannotation. - An
@Idfield that maps to the primary key. - A no-argument constructor, which may be
protected. JPA uses it to create instances when it reads rows. - A class that is not
final, with nofinalpersistent fields. Hibernate creates subclass proxies for lazy loading, andfinalblocks that.
A first mapping
import jakarta.persistence.*;
@Entity
@Table(name = "authors")
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 100)
private String name;
protected Author() {
}
public Author(String name) {
this.name = name;
}
public Long getId() { return id; }
public String getName() { return name; }
}The table below shows what each annotation controls.
| Annotation | Purpose |
|---|---|
| `@Entity` | Marks the class as persistent. |
| `@Table(name = "authors")` | Sets the table name. Without it, the table name comes from the class name. |
| `@Id` | Marks the primary key field. |
| `@GeneratedValue` | Lets the database or JPA produce the key. The next chapter covers the strategies. |
| `@Column` | Sets the column name, nullability and length. |
Default naming and @Column
Without @Table or @Column, Spring Boot applies a naming strategy. It converts camelCase names to snakecase, so publishedYear maps to `publishedyear. You only need @Column(name = ...)` when the column name differs from that default.
The nullable and length attributes are used in two places. When Hibernate generates the schema, it creates NOT NULL and varchar(100). At runtime, Hibernate also uses nullable = false for some checks. Treat them as documentation of your schema, but do not rely on them for input validation. Use Bean Validation for that.
Fields that are not columns
Every non-static, non-transient field is persistent by default. To exclude one, mark it @Transient:
@Transient
private String displayLabel;JPA also ignores static fields and fields declared with the Java transient keyword.
Access type
JPA reads state either from fields or from getters. The placement of @Id decides which. If @Id is on a field, as above, JPA uses field access. If it is on a getter, JPA uses property access. Pick field access and stay consistent. Mixing annotation placement leads to mappings that are silently ignored.
Common errors
No default constructor for entity: add aprotectedno-argument constructor.Table "AUTHORS" not found: the schema was not created. Check yourspring.jpa.hibernate.ddl-autosetting or your migration tool.- Reserved word as a name: a class named
UserorOrdermaps to a table name that is reserved in many databases. Use@Table(name = "users")to avoid it. - Missing
@Id: Hibernate refuses to start with a message that the entity has no identifier.
Exercise
A class Order has a field private String note; that must not be stored, and the table must be named orders. Which annotations do you add, and where?
Show answer
Add @Entity and @Table(name = "orders") on the class, and @Transient on the note field. @Table avoids the reserved word order. @Transient excludes the field from the mapping. The class also needs an @Id field and a no-argument constructor.