๐งช Exercises โ JPA and Hibernate
These exercises are designed to help you practice and internalize JPA and Hibernate concepts.
๐ Complete all exercises before moving to the assignment.
๐ฏ Exercise Guidelines
- Write clean, readable code
- Follow layered architecture (Controller โ Service โ Repository)
- Test APIs using Postman
- Commit code after each exercise
- Do NOT copy from internet โ try first
๐งฉ Exercise 1: Create Your First Entity
Objective
Understand how to map a Java class to a database table.
Task
- Create an entity called
Student - Add fields:
- id (Primary Key)
- name
- Use appropriate JPA annotations
Expected Outcome
- Table
studentsshould be created automatically - Data should be stored in database
๐งฉ Exercise 2: Create Repository and Perform CRUD
Objective
Learn how to use Spring Data JPA repositories.
Task
- Create
StudentRepositoryextendingJpaRepository - Implement:
- Save a student
- Fetch all students
- Delete a student by ID
Expected Outcome
- CRUD operations should work via API
๐งฉ Exercise 3: Build REST APIs
Objective
Expose database operations via REST endpoints.
Task
Create APIs:
- POST
/studentsโ Create student - GET
/studentsโ Get all students - DELETE
/students/{id}โ Delete student
Expected Outcome
- APIs should work via Postman
- Data should persist in DB
๐งฉ Exercise 4: Custom Query Methods
Objective
Understand how to write query methods.
Task
Add methods in repository:
- Find student by name
- Find students containing a keyword in email
Example:
findByName(String name)
findByEmailContaining(String keyword)
Expected Outcome
- APIs should return filtered data
๐งฉ Exercise 5: Add Entity Relationship
Objective
Understand relationships in JPA.
Task
- Create a
Courseentity:- id
- courseName
- Establish relationship:
- One Student โ Many Courses
- Use:
@OneToMany@ManyToOne
Expected Outcome
- Courses linked to students
- Foreign key created in database
๐งฉ Exercise 6: Implement Fetch Types
Objective
Understand LAZY vs EAGER loading.
Task
- Set relationship to LAZY loading
- Fetch student data
- Observe behavior when accessing courses
Expected Outcome
- Courses should load only when accessed
- Understand performance impact
๐งฉ Exercise 7: Handle Common Issues
Objective
Learn debugging in JPA.
Task
- Intentionally break:
- Remove
@Transactional - Access lazy data outside service
- Remove
- Observe errors
- Fix them
Expected Outcome
- Understand LazyInitializationException
- Learn correct usage of transactions
๐งฉ Exercise 8: Add Validation (Basic)
Objective
Ensure data integrity.
Task
- Add validation:
- name should not be empty
- email should be valid
- Use annotations like:
@NotNull@Email
Expected Outcome
- Invalid data should be rejected
๐งฉ Exercise 9: Logging (Bonus)
Objective
Improve observability.
Task
- Add logs in service layer:
- When saving student
- When fetching data
- Use proper log levels:
- INFO
- ERROR
Expected Outcome
- Logs should appear in console
๐ง Reflection Questions
After completing exercises, think:
- Why do we use ORM instead of SQL?
- When should we use LAZY vs EAGER?
- What happens if relationships are not mapped correctly?
- Why should controllers not directly access repositories?
๐ Next Step
Once done, move to:
๐ Quiz
Test your understanding before starting the assignment.
๐ฅ Why this is strong
- Gradual progression (Entity โ CRUD โ API โ Relationships โ Debugging)
- Includes intentional failure exercise (very powerful learning)
- Connects to real backend thinking
- Reinforces testing mindset