Lesson 10 · Data Management · completes Section 2

Spring Data JPA

Repositories you don't implement, queries derived from method names, and how Spring Boot wires it all up.

What you'll be tested on The repository interface hierarchy and what each adds; how a repository is implemented (you don't write it); which method names are valid derived queries and the keywords they use; and @Query for explicit JPQL / native SQL.

Spring Boot JPA setup

Add spring-boot-starter-data-jpa and a JDBC driver. Boot then auto-configures the DataSource, the EntityManagerFactory (Hibernate by default), a JpaTransactionManager, and enables repositories — you write almost no plumbing.

@Entity
public class Account {
  @Id @GeneratedValue
  private Long id;
  private String email;
  // ...
}

Configure the database in application.properties (spring.datasource.url/username/password). With H2 on the classpath and no URL, Boot even spins up an in-memory database automatically.

The repository hierarchy

InterfaceAdds
Repository<T,ID>Marker interface — no methods
CrudRepositorysave, findById, findAll, delete, count, existsById
PagingAndSortingRepositoryfindAll(Pageable), findAll(Sort)paging & sorting
JpaRepositoryJPA extras: flush, saveAll, findAll returning List, batch deletes

Each extends the one above it, so JpaRepository has everything.

You declare it; Spring implements it You write only an interface — Spring Data generates a proxy implementation at runtime. No @Repository annotation, no implementing class.
public interface AccountRepository extends JpaRepository<Account, Long> {
  // derived queries — no body needed:
  List<Account> findByEmail(String email);
  List<Account> findByActiveTrueOrderByEmailAsc();
}

Derived query methods

Spring parses the method name into a query. The name = a prefix + By + property conditions:

PartOptions
Prefixfind…By, read…By, get…By, query…By, count…By, exists…By, delete…By
ConditionsAnd, Or, Between, LessThan, GreaterThan, Like, StartingWith, In, IsNull, True/False, IgnoreCase
OrderingOrderBy<Property>Asc / Desc
Return typesthe entity, Optional<T>, List<T>, Page<T>, Stream<T>, long (for count)

Examples: findByLastName(String), findByAgeGreaterThan(int), findByLastNameAndFirstName(...), countByActiveTrue().

Explicit queries with @Query

When a derived name would be unwieldy, write the query yourself. JPQL by default; native SQL with a flag:

@Query("select a from Account a where a.email = :email")
Account lookup(@Param("email") String email);          // JPQL + named param

@Query(value = "select * from account where email = ?1", nativeQuery = true)
Account lookupNative(String email);                        // native SQL
Primary source — read this
Spring Data JPA 2021.0 — Query Methods & Query Creation from Method Names

Read "Core concepts", "Defining Query Methods", and the "Query Creation" keyword table.

Check yourself

Derived-query validity and the repository hierarchy are the money questions. Covers your book's whole Spring Data JPA section. Options shuffle on every load.

🎉 Section 2 (Data Management) complete You've now covered JDBC, transactions, and Spring Data JPA. Second pass: your book's Spring Data JPA section (8 questions). Ask me any time for a Data Management mixed review.
I'm your teacher — ask me anything. Want to see a full entity + repository + derived-query example, or the difference between JpaRepository and CrudRepository in practice? Ask. Say "continue" to start Section 3 — Spring MVC with Lesson 11.
← Lesson 9 · Transactions Lesson 11 · Spring MVC → (coming next)