Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 29

    Entity Mapping

    Entities are Java classes annotated with @Entity that map 1-to-1 to database tables.

    Course progress0%
    Focus
    3 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Entities are Java classes annotated with @Entity that map 1-to-1 to database tables. Get the mapping right and JPA stays out of your way.

    Informative example

    ts
    @Entity
    @Table(name = "orders", indexes = {
    @Index(name = "ix_orders_user", columnList = "user_id"),
    @Index(name = "ix_orders_status", columnList = "status")
    })
    public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "user_id", nullable = false)
    private Long userId;
    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 16)
    private OrderStatus status;
    @Column(name = "total_cents", nullable = false)
    private int totalCents;
    @Column(name = "created_at", nullable = false, updatable = false)
    private Instant createdAt = Instant.now();
    // getters / setters or @Getter @Setter from Lombok
    }

    Best practices

    • @Enumerated(EnumType.STRING)never ORDINAL (renaming/reordering breaks data).
    • Index FK columns and frequent filters explicitly with @Index.
    • updatable = false for created_at — protect against accidental writes.
    Ready to mark this lesson complete?Track your journey across the entire course.