Entities and H2 in Spring Boot: a Post Class Becomes a Table
The blog's cards are typed into HTML. A Post class with @Entity and @Id becomes the POST table in an in-memory H2 database: three pom lines, a few YAML settings, and Hibernate writes the create table SQL at startup.
What you will learn
- Map a class to a table, a field to a column and an object to a row
- Follow an insert and a select between a Post object and the POST table
- Add Spring Data JPA, H2 and the H2 console to a Boot 4 project
- Check the generated table in the H2 console
Before you start
- From Design Frames to Thymeleaf Pages
- Entity Classes and CRUD with the Hibernate Session
Our blog posts are still typed straight into home.html: a title, a category, an excerpt and a date per card. Nothing is saved anywhere. Today a Post gets a proper home, a table, and Hibernate does the translating between the two.
Class to table, field to column, object to row
An insert: new Post(title, category, excerpt, date) fills the fields; the id is still null because nobody has counted yet. On save, Hibernate writes an insert with four question marks for the four values, and the id says default: the database decides. The row lands with number 1, and that 1 flies back into the object.
A select goes the other way. The row comes back from H2, Hibernate calls the empty constructor to make an empty Post, and fills each field from its column. That's why the class needs an empty constructor.
pom.xml: three new dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-h2console</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
The data JPA starter brings Hibernate along. spring-boot-h2console is a web page for looking inside the database, and h2 is the small database itself, living in memory.
application.yaml
spring:
application:
name: StudyEasyblog
datasource:
url: jdbc:h2:mem:blogdb # a database in memory, named blogdb
jpa:
hibernate:
ddl-auto: create-drop # make the tables at startup, drop them at shutdown
show-sql: true # print every SQL statement
h2:
console:
enabled: true # http://localhost:8080/h2-console
create-drop makes the tables when the app starts and drops them when it stops, which suits a database that lives in memory anyway. show-sql prints every statement, so you can watch the inserts and selects.
Post.java
package org.studyeasy.studyeasyblog.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.time.LocalDate;
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String category;
private String excerpt;
private LocalDate publishedOn;
protected Post() {
}
public Post(String title, String category, String excerpt, LocalDate publishedOn) {
this.title = title;
this.category = category;
this.excerpt = excerpt;
this.publishedOn = publishedOn;
}
public Long getId() { return id; }
public String getTitle() { return title; }
public String getCategory() { return category; }
public String getExcerpt() { return excerpt; }
public LocalDate getPublishedOn() { return publishedOn; }
}
@Entity says "make a table for this class". @Id marks the row number, and GenerationType.IDENTITY lets the database count it. The protected empty constructor is for Hibernate; the four-value constructor is for our own code; the getters let the other layers read the fields. This is the same mapping you saw in plain Hibernate, now with Spring Boot doing the setup.
Hibernate writes the SQL
At startup, Hibernate looks for every class marked @Entity, writes a create table post statement with one column per field, and H2 runs it. You wrote none of that SQL. Run the app (mvnw spring-boot:run, or StudyEasyblogApplication in IntelliJ), and the log shows that create table line.
Open http://localhost:8080/h2-console, use the JDBC URL jdbc:h2:mem:blogdb with H2's default user sa and no password, and connect. The POST table is there, empty, with the columns id, title, category, excerpt and published_on, and id as the primary key.
Next, a repository and a service layer save and read these rows, and then seed data fills the homepage from them.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Spring Boot Repository and Service Layer: Save and Read Data Without Writing SQL
A one-line JpaRepository interface and a small @Service class save and read blog posts with no SQL. Follow one save and one read through every layer, then prove it with a test.
Seed Data and Posts on the Homepage: CommandLineRunner, the Model and th:each
SeedData, a CommandLineRunner, saves three posts when the POST table is empty. HomeController puts postService.getAll() into the Model, and one postCard inside th:each becomes three cards. Three inserts at startup, one select per visit.
Pagination and Sorting on a Spring Boot Homepage: PageRequest In, Page Out
Eight posts, three to a page, newest first. HomeController turns ?page=1&sort=oldest into a PageRequest, PostService passes it to findAll(Pageable), H2 runs one select with an offset and one count, and the Page that comes back knows it is page 2 of 3.
Connect Spring Boot to PostgreSQL: Driver, application.yml and the Hikari Pool
Connect the Relay support desk to PostgreSQL 18: one compose service, one driver line in the pom, a datasource in application.yml with a strict prod profile, and a small runner that asks the server who it is through the Hikari pool.
