← All tutorials

GoCareerGo Tutorials

Spring Boot Tutorial

Auto-configuration, REST APIs, JPA, AOP, caching, microservices and Spring Cloud.

Spring Data JPA

Lets you talk to a database through a Java interface — Spring generates the implementation and the SQL for you.

public interface UserRepository extends JpaRepository<User, Long> {
    Optional<User> findByEmail(String email);
    List<User> findByAgeGreaterThan(int age);
}
  • Extend JpaRepository<Entity, IdType> to get save/findById/findAll/delete for free
  • Method names like findByEmail are parsed into SQL automatically (derived queries)
  • @Query lets you write JPQL or native SQL when derived queries aren't enough