AI Hints
Home›Spring Boot Tutorial›Entities, Ids and Generated Values and Repositories
Spring Boot · Lesson 49 of 120

Entities

Classes that become tables.

⏱ 2 min read▶ Video at 1:05:45🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

An entity is a Java class that JPA saves in a table. Each object of the class becomes one row.

  • A class
  • Saved in a table
  • One object, one row

Book as an entity

At Entity tells JPA, save books in a table. At Id marks the primary key, the unique id of each row. The other fields become columns. JPA needs an empty constructor, which can be protected. And our own constructor makes new books, without an id.

Book.javajava
@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    private double price;

    protected Book() {}

    public Book(String title, String author, double price) {
        this.title = title; this.author = author; this.price = price;
    }

    // getters, setPrice and toString follow
}

Entity rules

An entity must be a class, not a record, because JPA fills its fields. It needs an at Id field. It needs a constructor with no arguments. And it usually has getters, so Jackson can turn it into JSON.

keyvalue
A classnot a record
@Ida primary key
No-args constructorprotected is fine
Gettersfor JSON

The table JPA creates

Show SQL equals true, prints every SQL statement that Hibernate runs. With H2, Hibernate creates the tables for us, at startup. Run it. Hibernate created a table called book. It has an id column, generated by identity, and one column for each field.

application.propertiesini
spring.jpa.show-sql=true
Output
Hibernate: create table book (price float(53) not null, id bigint generated by default
as identity, author varchar(255), title varchar(255), primary key (id))
Also uses Book.java
@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    private double price;

    protected Book() {}

    public Book(String title, String author, double price) {
        this.title = title; this.author = author; this.price = price;
    }

    public Long getId() { return id; }
    public String getTitle() { return title; }
    public String getAuthor() { return author; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }

    @Override
    public String toString() { return "Book " + id + ": " + title + " (" + price + ")"; }
}
Also uses BookRepository.java
public interface BookRepository extends JpaRepository<Book, Long> {
}

Customising columns

At Table changes the table name. At Column can make a column required, or set its length. It can also rename the column. And at Transient means, don't save this field at all.

Book.javajava
@Entity
@Table(name = "books")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 200)
    private String title;

    @Column(name = "list_price")
    private double price;

    @Transient
    private String note;
}

Summary

At Entity maps a class to a table, at Id marks its primary key, and the other fields become columns. Next, ids, and generated values.

Key takeaways
  • @Entity
  • @Id
  • Fields → columns
Next up: Ids and Generated Values — who picks the id?