Spring Data JPA & repositories
Entities, repositories, derived query methods, H2/MySQL configuration and pagination.
JPA maps Java classes to database tables; Hibernate is the implementation Spring Boot uses. Spring Data JPA then generates repository code for you.
- An
@Entityclass is a table;@Idwith@GeneratedValueis the primary key. - Extend
JpaRepository<Job, Long>to getsave,findById,findAll,deleteByIdand paging for free. - Derived queries: name a method
findByCityIgnoreCaseand Spring writes the SQL. - Use
@Queryfor anything more complex.
The H2 in-memory database is handy for learning; switch to MySQL or PostgreSQL by changing spring.datasource.* properties and the driver dependency.
Example code
Spring Boot 3 · Java 17+@Entity
public class Job {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String city;
private double salaryLpa;
// constructors, getters and setters
}
public interface JobRepository extends JpaRepository<Job, Long> {
List<Job> findByCityIgnoreCase(String city);
List<Job> findBySalaryLpaGreaterThanOrderBySalaryLpaDesc(double min);
Page<Job> findByTitleContainingIgnoreCase(String q, Pageable pageable);
}
// application.properties (MySQL)
// spring.datasource.url=jdbc:mysql://localhost:3306/jobs
// spring.datasource.username=app
// spring.datasource.password=${DB_PASSWORD}
// spring.jpa.hibernate.ddl-auto=validate
Output / API response
jobRepository.findByCityIgnoreCase("kochi")
→ Hibernate: select ... from job j where upper(j.city)=upper(?)
→ [Job{id=1, title='Java Developer', city='Kochi'}]
jobRepository.findByTitleContainingIgnoreCase("java", PageRequest.of(0, 10))
→ Page 1 of 3 (25 jobs)
Generate a project at start.spring.io (Maven, Java 17+, dependencies: Spring Web, Spring Data JPA, H2, Validation), add the code, and run ./mvnw spring-boot:run. Learn the Java tutorial first if you're new to Java. Data is fictional.
Key points
- @Entity maps a class to a table.
- JpaRepository gives CRUD and paging methods for free.
- Method names like findByCity become queries automatically.
- Keep passwords in environment variables, not in properties files.
Quick check
What does findByCityIgnoreCase(String city) do?
Spring Data parses derived method names and builds the matching query automatically.
Practice task
Add an Application entity with a @ManyToOne link to Job, and a repository method that returns all applications for a given job id.
Interview corner
What is the difference between JPA, Hibernate and Spring Data JPA?
JPA is the specification for Java ORM. Hibernate is the most common implementation of it. Spring Data JPA sits on top and generates repository implementations, so you write interfaces instead of DAO code.
What does spring.jpa.hibernate.ddl-auto do?
It controls schema handling: create/create-drop build tables (for learning or tests), update alters them, validate only checks the schema matches, and none does nothing. Production apps use validate or none with migration tools such as Flyway or Liquibase.
Further reading
Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.
Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.