Spring Data JPA Basics

Declare an interface extending JpaRepository (or CrudRepository ) and Spring Data generates the implementation — giving you save , findById , findAll and delete with zero boilerplate.

Learn Spring Data JPA Basics in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Make sure you understand the repository pattern and @Repository , and ideally have seen JDBC to appreciate how much boilerplate Spring Data removes.

💡 Analogy: Writing data access by hand is like cooking every dish yourself. Spring Data JPA is like ordering from a menu : you simply name what you want (declare an interface, write a method like findByLastName ) and the kitchen (Spring Data) prepares it. save , findById , findAll and delete are the standing menu — always available, no recipe required.

You describe what you want; Spring Data implements the how .

Extend JpaRepository<Entity, Id> and Spring Data generates the implementation at runtime. Add derived query methods just by naming them.

Extending the base type gives you full CRUD — save , findById , findAll , count , deleteById — with no code to write.

Inject the repository interface into a service and call its methods. The same clean layering as before — service depends on repository — with the implementation provided automatically.

To make the generated methods tangible, here is a plain-Java stand-in with real output, showing exactly the save / findById / count / delete behaviour Spring Data gives you.

Answer: Spring Data generates it at runtime — you only declare the interface.

Answer: an Optional<T> , empty when no entity has that id.

Answer: the entity type and the id type — e.g. JpaRepository<User, Long> .

You now know how Spring Data JPA generates a repository implementation from an interface, the difference between JpaRepository and CrudRepository, the <Entity, Id> type parameters, the free CRUD methods (save/findById/findAll/delete), and derived query methods.

Next up: JPA Entities & Mapping — turning your classes into database tables with @Entity and friends.

Practice quiz

What does Spring Data JPA do for a repository interface?

  • Nothing automatic
  • Generates the implementation at runtime from the interface
  • Compiles it to native code
  • Renders it as HTML

Answer: Generates the implementation at runtime from the interface. You declare an interface extending a Spring Data base type, and Spring generates a working implementation at runtime.

Which interface adds JPA-specific features on top of CrudRepository?

  • MongoRepository
  • JpaRepository
  • WebRepository
  • HttpRepository

Answer: JpaRepository. JpaRepository extends PagingAndSortingRepository/CrudRepository and adds JPA-specific methods like flush and batch operations.

What are the two type parameters of JpaRepository<T, ID>?

  • The entity type and its id type
  • The table name and column name
  • The request and response
  • Two unrelated services

Answer: The entity type and its id type. JpaRepository<T, ID> is parameterised by the entity type T and the type of its primary key ID.

Which method does CrudRepository provide to persist an entity?

  • render()
  • save()
  • route()
  • scan()

Answer: save(). save() inserts or updates the given entity and returns the saved instance.

What does findById return in Spring Data?

  • the entity or null
  • an Optional<T>
  • a boolean
  • a List<T>

Answer: an Optional<T>. findById returns Optional<T>, which is empty when no entity has that id.

What does findAll() return?

  • a single entity
  • an Iterable/List of all entities
  • a count
  • void

Answer: an Iterable/List of all entities. findAll() returns all entities of the type as an Iterable (a List for JpaRepository).

How do you delete an entity by its id?

  • drop(id)
  • remove(id)
  • deleteById(id)
  • purge(id)

Answer: deleteById(id). deleteById(ID) removes the entity with the given primary key.

A method named findByLastName in a repository interface is a...

  • compile error
  • derived query method generated from its name
  • static method
  • controller mapping

Answer: derived query method generated from its name. Spring Data parses method names like findByLastName and derives the query automatically.

Do you write the implementation class for a JpaRepository interface?

  • Yes, always by hand
  • No — Spring Data provides it at runtime
  • Only in tests
  • Only for delete methods

Answer: No — Spring Data provides it at runtime. You only declare the interface; Spring Data generates and registers the implementation as a bean for you.

CrudRepository's count() returns...

  • the number of entities
  • the first entity
  • a boolean
  • the id

Answer: the number of entities. count() returns the total number of entities of that type as a long.