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.
Before You Start
Make sure you understand the repository pattern and @Repository , and ideally have seen JDBC to appreciate how much boilerplate Spring Data removes.
What You'll Learn
The Big Idea — Ordering From a Menu, Not Cooking
💡 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 .
1️⃣ Declare a Repository Interface
Extend JpaRepository<Entity, Id> and Spring Data generates the implementation at runtime. Add derived query methods just by naming them.
2️⃣ The Methods You Get for Free
Extending the base type gives you full CRUD — save , findById , findAll , count , deleteById — with no code to write.
3️⃣ Using the Generated Repository
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.
4️⃣ What CRUD Looks Like, Concretely
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.
🧠 Quick Recall
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> .
Common Errors
- ❌ Writing an implementation class. Unnecessary. Fix: declare only the interface; Spring Data does the rest.
- ❌ Wrong id type parameter. Method signatures break. Fix: match the entity's primary-key type.
- ❌ Treating findById as returning the entity. It's an Optional. Fix: unwrap with orElse/map.
- ❌ Mistyped derived method names. e.g. findByLstName . Fix: match the entity property names exactly.
- ❌ Overusing derived methods for complex queries. Fix: use @Query (JPQL/native) when names get unwieldy.
Pro Tips
- 💡 Extend JpaRepository for the full feature set on a JPA-backed app.
- 💡 Embrace Optional from findById — handle "not found" explicitly, no nulls.
- 💡 Name derived methods carefully — they must match entity property names.
- 💡 Drop to @Query for queries too complex to express as a method name.
📋 Quick Reference
Method / type
Signature / idea
Note
base type
JpaRepository<T, ID>
entity, id
save
save(entity)
insert/update
findById
Optional<T> findById(id)
empty if absent
findAll
List<T> findAll()
all rows
count
long count()
how many
deleteById
deleteById(id)
remove by key
derived
findByLastName(name)
from method name
📚 Resources / Keep Learning
- 📘 Spring Data JPA — Getting Started (docs)
- 📗 Derived Query Methods — Spring Data docs
- 📗 Accessing Data with JPA — spring.io guide
❓ Frequently Asked Questions
🎉 Lesson Complete!
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.
Continue this course
- Previous: Repositories & @Repository
- Next: JPA Entities & Mapping