AI Hints
Home›Spring Boot Tutorial›CRUD with JpaRepository, Seeding Data and Practice: Books in a Database
Spring Boot · Lesson 53 of 120

Seeding Data

Start with some rows.

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

An empty database is hard to test against. We want some books there, every time the app starts.

  • Empty is hard to test
  • Some books at start
  • Every time

data.sql

Put a file called data dot sql, in source main resources. Spring Boot runs it at startup. Here, one insert adds five books. We leave out the id, so the database numbers them.

data.sqlsql
INSERT INTO book (title, author, price) VALUES
  ('Dune', 'Frank Herbert', 9.99),
  ('The Hobbit', 'J.R.R. Tolkien', 8.99),
  ('Circe', 'Madeline Miller', 10.99),
  ('Children of Dune', 'Frank Herbert', 8.49),
  ('Song of Achilles', 'Madeline Miller', 9.49);

One important setting

By default, data dot sql runs before Hibernate creates the tables, so the insert would fail. Defer data source initialization equals true, runs it after the tables exist. With an in memory database, it runs on every start.

keyvalue
data.sqlsrc/main/resources
Problemruns before tables exist
Fixspring.jpa.defer-datasource-initialization=true
H2 in memoryruns every start

Five books at startup

The controller only lists books. Run it. All five books from data dot sql are there, numbered one to five.

BookController.javajava
@RestController
public class BookController {

    private final BookRepository books;

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

    @GetMapping("/books")
    public List<Book> all() { return books.findAll(); }
}
Output
[
  {"id":1,"title":"Dune","author":"Frank Herbert","price":9.99},
  {"id":2,"title":"The Hobbit","author":"J.R.R. Tolkien","price":8.99},
  {"id":3,"title":"Circe","author":"Madeline Miller","price":10.99},
  {"id":4,"title":"Children of Dune","author":"Frank Herbert","price":8.49},
  {"id":5,"title":"Song of Achilles","author":"Madeline Miller","price":9.49}
]
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> {
}
Also uses data.sql
INSERT INTO book (title, author, price) VALUES
  ('Dune', 'Frank Herbert', 9.99),
  ('The Hobbit', 'J.R.R. Tolkien', 8.99),
  ('Circe', 'Madeline Miller', 10.99),
  ('Children of Dune', 'Frank Herbert', 8.49),
  ('Song of Achilles', 'Madeline Miller', 9.49);
Also uses application.properties
spring.jpa.defer-datasource-initialization=true
Also uses _curl.sh
curl localhost:8080/books

Other ways to seed

A Command Line Runner can save entities, using the repository, like we did earlier. Schema dot sql can create tables yourself, if you don't want Hibernate to. And for real projects, tools like Flyway manage database changes, step by step.

keyvalue
CommandLineRunnerrepository.saveAll(...)
schema.sqlcreate tables yourself
Flyway / Liquibaseversioned changes
data.sqlquick and simple

Summary

Data dot sql seeds the database at startup. With Hibernate, set defer data source initialization to true. Next, some practice.

Key takeaways
  • data.sql
  • defer-datasource-initialization
  • Five books
Next up: Practice: Books in a Database — your turn