liveBook · Manning

Spring Data JDBC

As explained in Java Persistence with Spring Data and Hibernate epub

Spring Data JDBC is a Spring Data module that provides database access through JDBC with reduced persistence boilerplate.

Spring Data JDBC provides JDBC-based repositories without requiring applications to manage connections and exceptions through old-style JDBC service code. It keeps a simpler, more limited object–relational mapping model than Spring Data JPA: there are no sessions, dirty tracking, caching, or lazy loading. Load an entity through a query, then explicitly save it when it changes. section 15.0

Start with a user repository

In a Spring Boot application, add spring-boot-starter-data-jdbc and a MySQL driver. Supply a schema.sql that creates USERS, then map a User class with @Table("USERS"), @Id, and @Column. Declare interface UserRepository extends CrudRepository<User, Long> and add List<User> findAll(). For MySQL, set spring.sql.init.mode=always if the startup script must run; this example recreates the table at startup. section 15.1

Choose a query style according to the condition:

  • Derive queries from repository method names, such as findByUsername(String username) or findByRegistrationDateBetween(LocalDate start, LocalDate end). Names must match entity properties; an invalid name fails when the application context loads. Derived conditions cannot use joins. section 15.2.1
  • Use @Query for SQL the method name cannot derive. For example, countByUserId needs an explicit SELECT COUNT(*) FROM USERS_ADDRESSES WHERE USER_ID = :USER_ID because userId is not a property of its repository entity. SQL is database-specific rather than JPQL. section 15.3.4 section 15.0

Relationships require deliberate table and class design. A User reference to Address annotated @MappedCollection(idColumn = "USER_ID") models the one-to-one example. For many-to-many, introduce a USERS_ADDRESSES table and a mapped intermediary class rather than relying on JPA relationship annotations. Schema DDL remains the application's responsibility. section 15.3.1 section 15.3.4

Table 15.2 Spring Data JPA capabilities vs. Spring Data JDBC capabilities (view table figure)

Spring Data JPA

Spring Data JDBC

Database-independent and portable

Generally, database-specific

Introduces complexity through object/relational mapping (ORM)

Less complex, but still adheres to the Spring Framework principles

Automatic schema generation based on the entities

Schema generation through DDL commands on the side of the programmer

Query derivation since the first version

Query derivation since version 2.0

Queries annotated with JPQL code and native SQL

Only queries using native SQL

Can reuse classes with JPA annotations

Uses annotations from the org.springframework.data package

Models relationships between entities through annotations such as @OneToMany, @Embedded, etc.

Models relationships mostly on the side of the programmer, through the design of the classes

Caching and lazy loading

No caching, no lazy loading

Sessions and dirty tracking

No sessions, no dirty tracking

The capability comparison helps decide whether simpler JDBC repositories justify giving up JPA features such as portability and automatic schema generation. section 15.3.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works through project setup, derived and explicit queries, and relationship mappings, including schema and lifecycle consequences.
Examples
Uses a MySQL-backed user repository, date-range query methods, one-to-one addresses, and a many-to-many join table.
What it assumes
Starts from Java entity classes, a Spring Boot project, Maven dependencies, and an SQL database schema.
What you can do after
Build it from scratch with Spring Boot, then define repository queries and map one-to-one, one-to-many, many-to-many, or embedded data.
How it connects
Connects JDBC repositories to Spring Boot, SQL, query derivation, object–relational mapping, and Spring Data JPA.

Also explained in

Compare how 3 books explain Spring Data JDBC →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.