Day 8 · 1 Hour
Spring Data JPA
Let Spring write your SQL — method names become database queries automatically.
🔥 Real-World Hook (0–5 min)
Writing SQL manually for every operation is like writing assembly code instead of Java. You'd write 500 lines where Spring writes 5.
Spring Data JPA automatically generates SQL from method names. findByDeptAndGpaGreaterThan("CS", 3.5) — that's your entire query. Used by Infosys, TCS, and every major Java backend team.
📧 JPA = Java Persistence API (5–20 min)
💡 Analogy
JPA is a translator between Java objects and database tables. You work with objects. JPA automatically writes the SQL. @Entity tells JPA "this class is a database table". @Id marks the primary key.
💻 Live Coding — Student CRUD with JPA (20–40 min)
// Entity — maps to a database table @Entity @Table(name = "students") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String name; @Column(unique = true) private String email; private String dept; private double gpa; // getters + setters } // Repository — JPA generates SQL automatically public interface StudentRepository extends JpaRepository<Student, Long> { // Spring generates: SELECT * FROM students WHERE dept=? AND gpa>? List<Student> findByDeptAndGpaGreaterThan(String dept, double minGpa); // SELECT * FROM students ORDER BY gpa DESC List<Student> findAllByOrderByGpaDesc(); // Custom JPQL query @Query("SELECT s FROM Student s WHERE s.gpa > :minGpa") List<Student> findTopStudents(@Param("minGpa") double minGpa); } // Service @Service public class StudentService { @Autowired private StudentRepository repo; public Student save(Student s) { return repo.save(s); } public List<Student> getAll() { return repo.findAll(); } public void delete(Long id) { repo.deleteById(id); } }
🎯 You Code It (40–55 min)
- Create a new Spring Boot project with dependencies: Spring Web, Spring Data JPA, MySQL Driver
- Configure
application.propertieswith your MySQL database details - Create a
Courseentity: id, title, credits, instructor, maxStudents - Create
CourseRepository extends JpaRepositorywith methods:findByInstructor(),findByCreditsGreaterThan() - Create
CourseControllerwith full CRUD endpoints - Show: Create 3 courses via Postman, then GET all — verify they're persisted in MySQL
📄 Day 8 Cheat Sheet — Spring Data JPA
Entity Annotations
| @Entity | Mark as DB table |
| @Table(name="t") | Custom table name |
| @Id | Primary key |
| @GeneratedValue | Auto-increment |
| @Column(nullable=false) | NOT NULL |
| @Column(unique=true) | UNIQUE constraint |
JpaRepository Methods
| repo.save(entity) | INSERT or UPDATE |
| repo.findById(id) | Returns Optional |
| repo.findAll() | Get all rows |
| repo.deleteById(id) | Delete by id |
| repo.count() | Total rows |
| repo.existsById(id) | Check exists |
Method Name Queries
| findByName(String n) | WHERE name=n |
| findByNameAndDept | AND condition |
| findByGpaGreaterThan | WHERE gpa>x |
| findByNameContaining | LIKE %name% |
| findAllByOrderByGpaDesc | ORDER BY |
| countByDept(String d) | COUNT WHERE |
Custom Query
| @Query("SELECT e FROM Entity e...") | JPQL |
| @Query(nativeQuery=true,"SELECT...") | Raw SQL |
| @Param("name") | Bind parameter |
| @Modifying + @Transactional | UPDATE/DELETE query |
| Page<T> findAll(Pageable p) | Pagination |