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

Ids and Generated Values

Who picks the id?.

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

Every row needs a unique id. But who picks it? Usually not us. We let the database number the rows.

  • Every row needs an id
  • Who picks it?
  • The database

Generation strategies

At Generated Value tells JPA, the id is generated. Identity lets the database count up, one, two, three. Sequence uses a separate counter object, in the database. And UUID makes long random ids, which are unique everywhere.

StrategyHow
IDENTITYdatabase counts up (we use this)
SEQUENCEa separate counter
UUIDrandom, globally unique
AUTOHibernate chooses

Saving gives an id

This runner uses a repository, which we'll study next lesson. It creates Dune, and prints its id before saving. Then it saves Dune, and prints the id again. Then it saves Circe, and prints every book. Run it. Before saving, the id is null. After saving, Dune has id one. And Circe got id two.

Loader.javajava
@Component
public class Loader implements CommandLineRunner {

    private final BookRepository books;

    public Loader(BookRepository books) {
        this.books = books;
    }

    @Override
    public void run(String... args) {
        Book dune = new Book("Dune", "Frank Herbert", 9.99);
        System.out.println("Before save, id = " + dune.getId());
        books.save(dune);
        System.out.println("After save, id = " + dune.getId());
        books.save(new Book("Circe", "Madeline Miller", 10.99));
        books.findAll().forEach(System.out::println);
    }
}
Output
Before save, id = null
After save, id = 1
Book 1: Dune (9.99)
Book 2: Circe (10.99)
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> {
}

Why Long, not long

We use the Long class, not the small long type. That's because a new book has no id yet, and only Long can be null. Null means, not saved yet.

keyvalue
Long idcan be null
nullnot saved yet
After saveid is filled in
long idcan't be null, avoid

Summary

At Generated Value lets the database pick ids. With identity, they count up from one. A null id means, not saved yet. Next, repositories.

Key takeaways
  • @GeneratedValue
  • IDENTITY
  • null = not saved
Next up: Repositories — data access for free