Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Entities

An entity is a Java class whose objects are stored in a database table. One table row is one object, one column is one field. JPA annotations define the link between the class and the table, and Hibernate builds the SQL from them.

The post entity

We replace the Post record from the previous chapter with a class. Delete the old Post.java and create a new one:

// src/main/java/com/example/blog/Post.java
package com.example.blog;
 
import java.time.Instant;
 
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
 
@Entity
@Table(name = "posts")
public class Post {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @Column(nullable = false, length = 120)
    private String title;
 
    @Column(nullable = false, length = 10000)
    private String body;
 
    @Column(nullable = false)
    private String author;
 
    @Column(nullable = false)
    private Instant createdAt;
 
    protected Post() {
    }
 
    public Post(String title, String body, String author, Instant createdAt) {
        this.title = title;
        this.body = body;
        this.author = author;
        this.createdAt = createdAt;
    }
 
    public Long getId() {
        return id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String getBody() {
        return body;
    }
 
    public void setBody(String body) {
        this.body = body;
    }
 
    public String getAuthor() {
        return author;
    }
 
    public Instant getCreatedAt() {
        return createdAt;
    }
}

What the annotations mean

AnnotationMeaning
@EntityThe class is stored in the database
@Table(name = "posts")The table name; without it the table would be post
@IdThe primary key
@GeneratedValue(strategy = IDENTITY)The database assigns the id when the row is inserted
@Column(nullable = false, length = 120)A NOT NULL column up to 120 characters long

Fields without annotations become columns too. Hibernate turns names into SQL style: the createdAt field becomes the created_at column.

Why not a record

Hibernate creates an empty object first and then fills its fields from a table row. For that it needs a no-argument constructor, and for changes it needs mutable fields. Records have neither, so entities are ordinary classes. The no-argument constructor is protected: Hibernate needs it, and our own code uses the second constructor.

Only the title and the body have setters — the author and the creation date do not change after publishing.

What ended up in the database

When started with ddl-auto: update, Hibernate creates the table itself and prints the SQL:

Hibernate: create table posts (id bigint generated by default as identity, author varchar(255) not null, body varchar(10000) not null, created_at timestamp(6) with time zone not null, title varchar(120) not null, primary key (id))

Instant became timestamp with time zone, Long became bigint, and author got the default length of 255 characters. You can see the table in the /h2-console. There is no post store yet — the repository from the next lesson takes its place.

Common mistake

If you forget @Id, the application does not start:

Entity 'com.example.blog.Post' has no identifier (every '@Entity' class must declare or inherit at least one '@Id' or '@EmbeddedId' property)

Every entity needs a primary key. Check the import as well: the annotations come from the jakarta.persistence package. The old javax.persistence package from Spring Boot 2 examples does not work in modern versions.

Thanks for reading!

Donate to the site — we work hard for you.