Java Spring Boot Data JPA
In this page:
What is Spring Boot Data JPA?
Spring Data JPA significantly simplifies database access in Spring applications by acting as an abstraction layer over Hibernate. It eliminates most of the boilerplate code you'd otherwise write by hand for basic CRUD operations, letting you focus on your actual data model instead of repetitive query code.
Example: What is Spring Boot Data JPA?
import org.springframework.data.jpa.repository.JpaRepository;
public class Main {
static class User { Long id; String name; }
interface UserRepository extends JpaRepository<User, Long> {} // no implementation code required
public static void main(String[] args) {
System.out.println("Spring Data JPA eliminates hand-written CRUD boilerplate");
}
}
Login to try C/C++/Java/PHP code in the editor
Repository CRUD Methods
Spring Data JPA repository interfaces come with built-in operations like save(), findById(), and delete() available out of the box, simply by extending an interface like JpaRepository -- no implementation code required for these common cases.
Example: Repository CRUD Methods
import org.springframework.data.jpa.repository.JpaRepository;
public class Main {
static class User { Long id; String name; }
interface UserRepository extends JpaRepository<User, Long> {}
public static void main(String[] args) {
// userRepository.save(user); userRepository.findById(1L); userRepository.delete(user);
System.out.println("save(), findById(), delete() come built in");
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Query Methods
Beyond the built-in methods, you can declare custom query methods either by following Spring's method-name convention (like findByLastNameAndAgeGreaterThan) or by writing an explicit query with the @Query annotation for cases the naming convention can't express.
Example: Custom Query Methods
import org.springframework.data.jpa.repository.*;
import java.util.List;
public class Main {
static class User { String lastName; int age; }
interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLastNameAndAgeGreaterThan(String lastName, int age); // derived from the method name
}
public static void main(String[] args) {
System.out.println("Query derived automatically from the method name");
}
}
Login to try C/C++/Java/PHP code in the editor
JPA Transactions
Transactions ensure that a set of related database operations either all succeed together or all roll back completely if one fails, keeping your data consistent. Annotate a service method with @Transactional to have Spring manage the transaction boundary around it automatically.
Example: JPA Transactions
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
public class Main {
@Service
static class UserService {
@Transactional // all writes inside commit together, or all roll back
void createUser(String name) {
System.out.println("Creating " + name + " inside a managed transaction");
}
}
public static void main(String[] args) {
new UserService().createUser("Riya");
}
}
Login to try C/C++/Java/PHP code in the editor
Pagination and Sorting
Spring Data JPA provides simple built-in utilities, via the Pageable and Sort types, to page through large result sets a chunk at a time and sort them dynamically by any field, without you writing manual LIMIT/OFFSET SQL.
Example: Pagination and Sorting
import org.springframework.data.domain.*;
public class Main {
public static void main(String[] args) {
Pageable pageable = PageRequest.of(0, 10, Sort.by("name")); // page 0, 10 per page, sorted by name
System.out.println(pageable);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: