0–5 min
Real Hook
5–20 min
Core Concept
20–40 min
Live Coding
40–55 min
You Code It
55–60 min
Cheat Sheet
🔥 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)
  1. Create a new Spring Boot project with dependencies: Spring Web, Spring Data JPA, MySQL Driver
  2. Configure application.properties with your MySQL database details
  3. Create a Course entity: id, title, credits, instructor, maxStudents
  4. Create CourseRepository extends JpaRepository with methods: findByInstructor(), findByCreditsGreaterThan()
  5. Create CourseController with full CRUD endpoints
  6. Show: Create 3 courses via Postman, then GET all — verify they're persisted in MySQL

📄 Day 8 Cheat Sheet — Spring Data JPA

Entity Annotations

@EntityMark as DB table
@Table(name="t")Custom table name
@IdPrimary key
@GeneratedValueAuto-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
findByNameAndDeptAND condition
findByGpaGreaterThanWHERE gpa>x
findByNameContainingLIKE %name%
findAllByOrderByGpaDescORDER 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 + @TransactionalUPDATE/DELETE query
Page<T> findAll(Pageable p)Pagination