📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Spring Data
JpaRepository Interface
Spring Boot JpaRepository Example
Create a Spring Boot Application
Pom Dependencies - pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.guides.springboot2</groupId>
<artifactId>springboot-jparepository-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot2-jpa-crud-example</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
resources/application.properties
spring.main.banner-mode=off
logging.pattern.console=%clr(%d{yy-MM-dd E HH:mm:ss.SSS}){blue} %clr(%-5p) %clr(%logger{0}){blue} %clr(%m){faint}%n
JPA Entity - Employee.java
package net.guides.springboot.jparepository.model;
import jakarta.persistence.*;
@Entity
@Table(name = "employees")
public class Employee {
private long id;
private String firstName;
private String lastName;
private String emailId;
public Employee() {
}
public Employee(String firstName, String lastName, String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "first_name", nullable = false)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name", nullable = false)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "email_address", nullable = false)
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId
+ "]";
}
}
JPA Repository - EmployeeRepository.java
package net.guides.springboot.jparepository.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.guides.springboot.jparepository.model.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
}
MyRunner.java
package net.guides.springboot.jparepository;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import net.guides.springboot.jparepository.model.Employee;
import net.guides.springboot.jparepository.repository.EmployeeRepository;
@Component
public class MyRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(MyRunner.class);
@Autowired
private EmployeeRepository employeeRepository;
@Override
public void run(String... args) throws Exception {
employeeRepository.save(new Employee("Ramesh", "Fadatare", "ramesh@gmail.com"));
employeeRepository.save(new Employee("Tom", "Cruise", "tom@gmail.com"));
employeeRepository.save(new Employee("John", "Cena", "john@gmail.com"));
employeeRepository.save(new Employee("tony", "stark", "stark@gmail.com"));
logger.info("# of employees: {}", employeeRepository.count());
logger.info("All employees unsorted:");
List<Employee> employees = employeeRepository.findAll();
logger.info("{}", employees);
logger.info("------------------------");
logger.info("All employees sorted by name in descending order");
List<Employee> sortedEmployees = employeeRepository.findAll(new Sort(Sort.Direction.DESC, "firstName"));
logger.info("{}", sortedEmployees);
logger.info("------------------------");
logger.info("Deleting all employees");
employeeRepository.deleteAllInBatch();
logger.info("# of employees: {}", employeeRepository.count());
}
}
Running Spring Boot Application
package net.guides.springboot.jparepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Output
19-06-30 Sun 11:21:04.623 INFO MyRunner # of employees: 4
19-06-30 Sun 11:21:04.623 INFO MyRunner All employees unsorted:
19-06-30 Sun 11:21:04.666 INFO MyRunner [Employee [id=1, firstName=Ramesh, lastName=Fadatare, emailId=ramesh@gmail.com], Employee [id=2, firstName=Tom, lastName=Cruise, emailId=tom@gmail.com], Employee [id=3, firstName=John, lastName=Cena, emailId=john@gmail.com], Employee [id=4, firstName=tony, lastName=stark, emailId=stark@gmail.com]]
19-06-30 Sun 11:21:04.667 INFO MyRunner ------------------------
19-06-30 Sun 11:21:04.667 INFO MyRunner All employees sorted by name in descending order
19-06-30 Sun 11:21:04.682 INFO MyRunner [Employee [id=4, firstName=tony, lastName=stark, emailId=stark@gmail.com], Employee [id=2, firstName=Tom, lastName=Cruise, emailId=tom@gmail.com], Employee [id=1, firstName=Ramesh, lastName=Fadatare, emailId=ramesh@gmail.com], Employee [id=3, firstName=John, lastName=Cena, emailId=john@gmail.com]]
19-06-30 Sun 11:21:04.682 INFO MyRunner ------------------------
19-06-30 Sun 11:21:04.682 INFO MyRunner Deleting all employees
19-06-30 Sun 11:21:04.691 INFO MyRunner # of employees: 0
19-06-30 Sun 11:21:04.623 INFO MyRunner # of employees: 4
19-06-30 Sun 11:21:04.623 INFO MyRunner All employees unsorted:
19-06-30 Sun 11:21:04.666 INFO MyRunner [Employee [id=1, firstName=Ramesh, lastName=Fadatare, emailId=ramesh@gmail.com], Employee [id=2, firstName=Tom, lastName=Cruise, emailId=tom@gmail.com], Employee [id=3, firstName=John, lastName=Cena, emailId=john@gmail.com], Employee [id=4, firstName=tony, lastName=stark, emailId=stark@gmail.com]]
19-06-30 Sun 11:21:04.667 INFO MyRunner ------------------------
19-06-30 Sun 11:21:04.667 INFO MyRunner All employees sorted by name in descending order
19-06-30 Sun 11:21:04.682 INFO MyRunner [Employee [id=4, firstName=tony, lastName=stark, emailId=stark@gmail.com], Employee [id=2, firstName=Tom, lastName=Cruise, emailId=tom@gmail.com], Employee [id=1, firstName=Ramesh, lastName=Fadatare, emailId=ramesh@gmail.com], Employee [id=3, firstName=John, lastName=Cena, emailId=john@gmail.com]]
19-06-30 Sun 11:21:04.682 INFO MyRunner ------------------------
19-06-30 Sun 11:21:04.682 INFO MyRunner Deleting all employees
19-06-30 Sun 11:21:04.691 INFO MyRunner # of employees: 0
Comments
Post a Comment
Leave Comment