ShopLite: add Spring Data JPA and serve categories
60 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Moving one feature from JdbcClient to Spring Data JPA means a new entity and a new repository behind an unchanged service contract: the controller, the JSON and the error responses stay exactly the same, and only the SQL in the log changes author.
What you'll build
You start from your shoplite-api repository at the end of Section 15 (release 1.0.0): every feature runs on JdbcClient against the Section 12 MySQL schema, with ProblemDetail errors, dev and prod profiles, OpenAPI docs and integration tests. In this project the category feature moves to Spring Data JPA. Products, customers and orders keep their JdbcClient repositories for now; the next modules move them one at a time.
You add the JPA starter, switch the configuration to YAML with the database settings from environment variables per profile, map Category as an @Entity with Integer ids (the columns are INT), and replace JdbcCategoryRepository with a CategoryRepository that extends JpaRepository. CategoryService keeps every public method, so CategoryController and ProductService do not change. Hibernate only validates the schema (ddl-auto: validate), the dev profile logs every SQL statement with its values, and you check all of it with curl and the existing tests.
$ java -jar target/shoplite-api-1.0.0.jar
...
2026-10-16T13:02:44.559+05:30 INFO 2828 --- [shoplite-api] [ main] com.shoplite.ShopliteApiApplication : No active profile set, falling back to 1 default profile: "dev"
2026-10-16T13:02:48.876+05:30 INFO 2828 --- [shoplite-api] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 157 ms. Found 1 JPA repository interface.
...
2026-10-16T13:02:57.086+05:30 INFO 2828 --- [shoplite-api] [ main] com.zaxxer.hikari.HikariDataSource : ShopLitePool - Start completed.
2026-10-16T13:02:57.475+05:30 INFO 2828 --- [shoplite-api] [ main] org.hibernate.orm.connections.pooling : HHH10001005: Database info:
Database JDBC URL [jdbc:mysql://localhost:3306/shoplite]
...
2026-10-16T13:03:03.808+05:30 WARN 2828 --- [shoplite-api] [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
...
2026-10-16T13:03:06.713+05:30 INFO 2828 --- [shoplite-api] [ main] com.shoplite.ShopliteApiApplication : Started ShopliteApiApplication in 24.729 seconds (process running for 27.324)
2026-10-16T13:03:07.101+05:30 INFO 2828 --- [shoplite-api] [ main] c.shoplite.common.DatabaseStartupCheck : Connected to MySQL 9.4.0, database shoplite
$ curl -i http://localhost:8080/api/v1/categories
HTTP/1.1 200
Content-Type: application/json
Content-Length: 299
[{"id":1,"name":"Electronics","parentId":null},{"id":2,"name":"Mobiles","parentId":1},{"id":3,"name":"Laptops","parentId":1},{"id":4,"name":"Accessories","parentId":1},{"id":5,"name":"Books","parentId":null},{"id":6,"name":"Fashion","parentId":null},{"id":7,"name":"Home & Kitchen","parentId":null}]
$ curl -i http://localhost:8080/api/v1/categories/2
HTTP/1.1 200
Content-Type: application/json
Content-Length: 38
{"id":2,"name":"Mobiles","parentId":1}
Server log for that request (dev profile):
2026-10-16T13:03:10.738+05:30 DEBUG 2828 --- [shoplite-api] [io-18631-exec-1] org.hibernate.SQL :
select
c1_0.category_id,
c1_0.name,
c1_0.parent_id
from
categories c1_0
where
c1_0.category_id=?
2026-10-16T13:03:10.741+05:30 TRACE 2828 --- [shoplite-api] [io-18631-exec-1] org.hibernate.orm.jdbc.bind : binding parameter (1:INTEGER) <- [2]
$ curl -i http://localhost:8080/api/v1/categories/99
HTTP/1.1 404
Content-Type: application/problem+json
{"type":"https://shoplite.example/problems/not-found","title":"Resource not found","status":404,"detail":"Category 99 was not found.","instance":"/api/v1/categories/99"}
$ curl -i -X POST http://localhost:8080/api/v1/categories -H "Content-Type: application/json" -d '{"name": "Smart Watches", "parentId": 1}'
HTTP/1.1 201
Location: http://localhost:8080/api/v1/categories/8
Content-Type: application/json
{"id":8,"name":"Smart Watches","parentId":1}
$ curl -i -X POST http://localhost:8080/api/v1/categories -H "Content-Type: application/json" -d '{"name": "mobiles", "parentId": 1}'
HTTP/1.1 409
Content-Type: application/problem+json
{"type":"https://shoplite.example/problems/already-exists","title":"Resource already exists","status":409,"detail":"A category named mobiles already exists.","instance":"/api/v1/categories"}
$ curl -i -X PUT http://localhost:8080/api/v1/categories/8 -H "Content-Type: application/json" -d '{"name": "Smartwatches", "parentId": 1}'
HTTP/1.1 200
Content-Type: application/json
Content-Length: 43
{"id":8,"name":"Smartwatches","parentId":1}
Server log for the PUT:
2026-10-16T13:03:15.103+05:30 DEBUG 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.SQL :
select
c1_0.category_id,
c1_0.name,
c1_0.parent_id
from
categories c1_0
where
c1_0.category_id=?
2026-10-16T13:03:15.104+05:30 TRACE 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.orm.jdbc.bind : binding parameter (1:INTEGER) <- [8]
2026-10-16T13:03:15.108+05:30 DEBUG 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.SQL :
select
count(*)
from
categories c1_0
where
c1_0.category_id=?
2026-10-16T13:03:15.109+05:30 TRACE 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.orm.jdbc.bind : binding parameter (1:INTEGER) <- [1]
2026-10-16T13:03:15.115+05:30 DEBUG 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.SQL :
select
c1_0.category_id,
c1_0.name,
c1_0.parent_id
from
categories c1_0
where
upper(c1_0.name)=upper(?)
2026-10-16T13:03:15.117+05:30 TRACE 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.orm.jdbc.bind : binding parameter (1:VARCHAR) <- [Smartwatches]
2026-10-16T13:03:15.136+05:30 DEBUG 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.SQL :
update
categories
set
name=?,
parent_id=?
where
category_id=?
2026-10-16T13:03:15.137+05:30 TRACE 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.orm.jdbc.bind : binding parameter (1:VARCHAR) <- [Smartwatches]
2026-10-16T13:03:15.137+05:30 TRACE 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.orm.jdbc.bind : binding parameter (2:INTEGER) <- [1]
2026-10-16T13:03:15.138+05:30 TRACE 2828 --- [shoplite-api] [io-18631-exec-3] org.hibernate.orm.jdbc.bind : binding parameter (3:INTEGER) <- [8]
$ curl -i -X DELETE http://localhost:8080/api/v1/categories/5
HTTP/1.1 409
Content-Type: application/problem+json
{"type":"https://shoplite.example/problems/in-use","title":"Resource in use","status":409,"detail":"Category 5 has products and cannot be deleted.","instance":"/api/v1/categories/5"}
$ curl -i -X DELETE http://localhost:8080/api/v1/categories/8
HTTP/1.1 204Requirements
- 1Work on a new branch,
jpa-categories;spring-boot-starter-jdbcis replaced byspring-boot-starter-data-jpa(no version), andmysql-connector-jstaysruntime. - 2The configuration is YAML:
application.yml(database fromDB_URL,DB_USER,DB_PASSWORD,ddl-auto: validate, the Jackson settings of the contract),application-dev.yml(local defaults and SQL logging),application-prod.yml(no SQL logging) andapplication-test.yml. - 3
Categoryis a package-private@Entitymapped tocategorieswithInteger id(category_id),nameandInteger parentId; the parent stays a plain id until the Relationships module. - 4
CategoryRepositoryextendsJpaRepository<Category, Integer>withfindByNameIgnoreCase,existsByParentIdand acountProductsnative query;JdbcCategoryRepositoryis deleted. - 5
CategoryServicekeeps all its public methods and their Long-based signatures; the controller still returnsCategoryResponserecords, the list is ordered by id, and all 404, 409 and 422 responses are unchanged. - 6Every category endpoint answers exactly as before when checked with curl, and the dev log shows the SQL Hibernate sends, with bound values.
- 7
./mvnw verifypasses all tests againstshoplite_test, which has the tables loaded once. - 8The README explains the data-access split (JPA for categories, JdbcClient for the rest), the SQL logging, the test-database step and the new startup errors.
Milestones
0/6 done · commit and push after each oneBranch, add the JPA starter and move the configuration to YAML
On branch jpa-categories, the app still starts on JdbcClient with application.yml, application-dev.yml and application-prod.yml.
Map Category as an entity
The app starts with ddl-auto: validate and Hibernate's log shows the categories mapping was accepted.
Replace JdbcCategoryRepository with a Spring Data repository
CategoryRepository extends JpaRepository, JdbcCategoryRepository is deleted, and the project compiles.
Keep CategoryService's contract
CategoryService compiles with the same public methods; CategoryController and ProductService are untouched.
Log the SQL in dev and check every endpoint
Every category curl call answers as before, the dev log shows each statement with its bound values, and ./mvnw verify passes.
Update the README and open a pull request
The README describes the JPA move, and a pull request from jpa-categories is open.
Build it in your own GitHub repository and push after each milestone. When everything works, mark the project as complete.
Extra challenges (optional)
Do any you like, each on its own branch, so your main project keeps working.
Turn on
hibernate.generate_statisticsin the dev profile for one run and count the statements ofDELETE /api/v1/categories/8. Write the count and what each statement does in the pull request.· easyAdd
CategoryRepositoryIT, an integration test againstshoplite_testthat checksfindByNameIgnoreCase("mobiles")finds category 2,existsByParentId(1)is true andcountProducts(5)is 3.· mediumLook at the SQL for the duplicate-name check: Spring Data writes
upper(c1_0.name)=upper(?). Explain in the README why that cannot use the UNIQUE index onname, and why the column's case-insensitive collation makes a plainfindByNameenough in MySQL.· medium
Solution
Opens when you add your repository link.
Finish up
Mark complete & continue
Next: @Entity, @Table, @Id and @GeneratedValue