Spring Data JPA Quick Start

Dependencies

Spring Data JPA uses Hibernate under the hood. Add the starter plus one JDBC driver:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- pick one (or more, e.g. H2 for dev and PostgreSQL for prod) -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

What You Get Automatically

  • A DataSource with a HikariCP connection pool, built from spring.datasource.*.
  • An EntityManagerFactory (Hibernate) that finds your @Entity classes.
  • A transaction manager, so @Transactional works.
  • Repository implementations generated at startup for every interface that extends JpaRepository.

Note: This module is a quick start. Relationships, fetching, JPQL, transactions and performance are covered in depth in the Hibernate & JPA material.