Spring Boot services · Chapter 16 of 41
JPA entities
By the end of this chapter you can map a class to a table with JPA, put your rules into the table Hibernate creates, and explain how Hibernate builds an object from a row. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Data
- 5 tasks
- Spring Boot
- In the all labs plan
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.
Why you need this
Ravi is adding a category (chai or snack) to the menu, and Anjali wants twenty new snacks. With JdbcClient, every new column means touching every query. JPA turns that around: you describe the class once, Hibernate writes the table, the inserts and the selects, and Spring Data writes the repository.
Ravi has already made MenuItem an entity. Start the starter with run-app --spring.jpa.show-sql=true and read the table Hibernate made from it:
Hibernate: create table menu_item (category tinyint check ((category between 0 and 1)), price integer not null, id bigint not null, name varchar(255), primary key (id))
Nobody gives the id a value, the name may be empty or 255 characters long, two items may share a name, and the category is a number. Saving the first item fails at once.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MenuItem.java: let the database generate the id
Make the database generate the id of every new MenuItem, with an identity column. After save, each item has an id that is not null, and two items saved one after the other get different ids.
Until this works every save fails, so do it first: tasks 3 to 5 save items too.
- Task 2
MenuItem.java: a required name of at most 40 characters
Make the menu_item.name column not nullable and 40 characters long, in the table Hibernate creates. Saving (and flushing) an item with a 41-character name then fails with a DataIntegrityViolationException.
- Task 3
MenuItem.java: refuse a second item with the same name
Make the database refuse two items with the same name: saving (and flushing) a second "Bun maska" fails with a DataIntegrityViolationException.
Needs task 1, since the test saves items.
- Task 4
MenuItem.java: store the category as CHAI or SNACK
Make the menu_item.category column store the enum's name: a saved SNACK item has 'SNACK' in that column, not the number 1.
Needs task 1, since the test saves an item.
- Task 5 · interview question
Interview: explain why an entity needs a no-argument constructor
Give MenuItem a constructor with no parameters that is neither public (only Hibernate should use it) nor private (Hibernate's proxies subclass the entity). An item saved and read back with findById then has its name, price and category (Parle-G, 10, SNACK), and an id that does not exist gives an empty Optional.
Needs task 1, since the test saves an item. Be ready to explain why Hibernate needs this constructor.
What you will learn
- @Entity and @Id: a class Hibernate maps to a table, and its primary key, with @GeneratedValue to let the database choose it.
- @Column: rules the table itself enforces.
- @Enumerated: how an enum is stored.
- The no-argument constructor: why Hibernate needs one (the interview question).
More in Data
Build "JPA entities" yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.