13 · Persistence / LESSON 59 OF 102
JPA entities and PostgreSQL
Map objects to rows while preserving SQL constraints.
Before this lesson: Outbound HTTP, timeouts, and failure handling
Understand the topic
Add Spring Data JPA and the PostgreSQL driver. Set SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD for a local database. Modern Spring uses jakarta.persistence imports.
Entities need an identifier and a no-argument constructor. Keep HTTP DTOs separate. For a disposable database only, spring.jpa.hibernate.ddl-auto=create-drop creates and later drops schema; never use that setting for real data. Migrations replace it later. Do not copy the earlier standalone Task classes into this package.
EXAMPLE · JAVA
src/main/java/com/example/tasks/Task.java
package com.example.tasks;
import jakarta.persistence.*;
@Entity
@Table(name = "task")
public class Task {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 120)
private String title;
@Column(nullable = false)
private boolean done;
protected Task() {}
public Task(String title) { this.title = title; }
public Long getId() { return id; }
public String getTitle() { return title; }
public boolean isDone() { return done; }
public void complete() { done = true; }
}What to expect
With JPA, datasource settings, and local schema creation, the application recognizes the entity. This alone creates no HTTP routes.
YOUR TURN
Put it into practice
- 01Inspect the local task table.
- 02Explain why a new object has a null ID.
- 03Explain object identity versus database identity.
Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.
UNDER THE HOOD
Build the mental model
An entity is a managed representation of database state, not an API contract or an ordinary immutable value.
- 01A new entity is transient until persisted. Managed entities participate in a persistence context; detached entities no longer automatically synchronize changes.
- 02Generated IDs arrive through persistence, so a newly constructed object may have a null ID. Avoid equality rules that change unexpectedly when that ID appears.
- 03Relations need explicit ownership and fetching decisions. Start with a unidirectional many-to-one from task to owner; never serialize an entire bidirectional graph directly.
A mistake worth avoiding
Lazy properties can trigger queries or fail after the persistence context closes.
DEBUGGING ROOM
Something broke. Where do you look?
JSON serialization recurses endlessly through owner.tasks and task.owner.
Before revealing the diagnosis, write down your first investigation step.
CHECK YOUR UNDERSTANDING
What is a DTO mainly for?
BUILD IT YOURSELF
Model task ownership
- 1.Add an owner relation with a non-null foreign key.
- 2.Create a migration for it.
- 3.Return owner-safe DTOs without serializing the relation graph.
Acceptance checklist
Lab completion is your self-report. Knowledge checks do not certify project quality.
RETRIEVE IT FROM MEMORY
What changes when an entity becomes detached?
Revisit this lesson in three days after a successful check or lab.
Ready for the next step?
Configure a datasource and explain an entity mapping.
Reading is separate from passing a knowledge check or completing a lab.