Why use Spring data JPA ?

In this blog, I am going to discuss about one of the newest and finest additions in the Spring group of projects which is Spring data jpa. For the uninitiated, the main advantage that it brings to the table is that it helps in reducing boiler plate JDBC code to a great extent and thereby it keeps codebase clean.

So what really is Spring data JPA ? Do we still need a JPA implementation like Hibernate / EclipseLink when we are using Spring data jpa. The answer to the last question is yes - we indeed still need a JPA implementation (like Hibernate) when we are using Spring data JPA.

Spring data JPA is NOT an alternate to Hibernate - we still need a JPA implementation like Hibernate. But our basic boiler plate code for a CRUD functionality is drastically reduced if we spring data JPA.

Let us see with a small example taken from my Github project SpringDataJPAWithoutBoot for this. Below are the steps required to create a spring-data jpa based project:

@Getter
@Setter
@ToString
@Entity
public class Customer {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String firstName;
	private String lastName;
	private int age;
	private double monthlySalary;
	private String country;
	private String language;
}
public interface CustomerRepository extends CrudRepository<Customer, Long> {
}
public class CustomerService {
	@Autowired
	private CustomerRepository customerRepository;

	public void addCustomer(Customer customer){
		customerRepository.save(customer);
	}
public interface CustomerRepository extends CrudRepository<Customer, Long> {
	List<Customer> findByLastName(String lastName);

	List<Customer> findByCountryAndLanguage(String country, String language);

	List<Customer> findByAgeGreaterThan(int age);
}

A couple of interesting things to note here: