-
Notifications
You must be signed in to change notification settings - Fork 0
JPA Repository
A JPA repository is an interface that extends one of the Spring Data repository interfaces such as JpaRepository
, CrudRepository
, or PagingAndSortingRepository
. The JpaRepository
interface provides JPA-related methods such as flushing the persistence context and deleting records in a batch.
To create a JPA repository, follow these steps:
-
Define an Entity:
import javax.persistence.Entity; import javax.persistence.Id; @Entity public class User { @Id private Long id; private String name; private String email; // Getters and setters }
-
Create the Repository Interface:
import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { }
Spring Data JPA allows you to define query methods by simply declaring methods in the repository interface. You can use naming conventions to create queries automatically.
These methods allow you to search for records based on specific attributes.
-
Example:
import java.util.List; public interface UserRepository extends JpaRepository<User, Long> { List<User> findByName(String name); List<User> findByEmail(String email); }
In this example, Spring Data JPA will automatically create the necessary queries to find User
entities by their name
or email
.
To find records where an attribute matches any value in a given list, use the In
keyword.
-
Example:
import java.util.List; public interface UserRepository extends JpaRepository<User, Long> { List<User> findByNameIn(List<String> names); }
In this example, findByNameIn
will generate a query to find all User
entities where the name
is in the provided list of names.
You can also create more complex queries using JPQL (Java Persistence Query Language) or native SQL.
-
Example:
import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface UserRepository extends JpaRepository<User, Long> { @Query("SELECT u FROM User u WHERE u.email = :email") User findByEmailAddress(@Param("email") String email); }
-
Example:
import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface UserRepository extends JpaRepository<User, Long> { @Query(value = "SELECT * FROM User WHERE email = :email", nativeQuery = true) User findByEmailNative(@Param("email") String email); }
Let's use a User
entity with additional attributes for demonstration:
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
private String email;
private Integer age;
private String city;
// Getters and setters
}
Here's the repository interface with various query methods:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Long> {
// Simple findBy methods
List<User> findByName(String name);
List<User> findByEmail(String email);
List<User> findByAge(Integer age);
// Using findIn
List<User> findByNameIn(List<String> names);
List<User> findByEmailIn(List<String> emails);
List<User> findByAgeIn(List<Integer> ages);
// Using findAllBy
List<User> findAllByCity(String city);
// Using findFirstBy
User findFirstByOrderByAgeAsc();
User findFirstByOrderByAgeDesc();
// Using JPQL
@Query("SELECT u FROM User u WHERE u.city = :city")
List<User> findUsersInCity(@Param("city") String city);
// Using Native SQL
@Query(value = "SELECT * FROM User WHERE city = :city", nativeQuery = true)
List<User> findUsersInCityNative(@Param("city") String city);
// Complex JPQL query
@Query("SELECT u FROM User u WHERE u.age > :age AND u.city = :city")
List<User> findUsersByAgeAndCity(@Param("age") Integer age, @Param("city") String city);
// Complex Native SQL query
@Query(value = "SELECT * FROM User WHERE age > :age AND city = :city", nativeQuery = true)
List<User> findUsersByAgeAndCityNative(@Param("age") Integer age, @Param("city") String city);
}
-
Simple
findBy
Methods:List<User> findByName(String name); List<User> findByEmail(String email); List<User> findByAge(Integer age);
-
findIn
Methods:List<User> findByNameIn(List<String> names); List<User> findByEmailIn(List<String> emails); List<User> findByAgeIn(List<Integer> ages);
-
findAllBy
Methods:List<User> findAllByCity(String city);
-
findFirstBy
Methods:User findFirstByOrderByAgeAsc(); User findFirstByOrderByAgeDesc();
-
JPQL Queries:
@Query("SELECT u FROM User u WHERE u.city = :city") List<User> findUsersInCity(@Param("city") String city); @Query("SELECT u FROM User u WHERE u.age > :age AND u.city = :city") List<User> findUsersByAgeAndCity(@Param("age") Integer age, @Param("city") String city);
-
Native SQL Queries:
@Query(value = "SELECT * FROM User WHERE city = :city", nativeQuery = true) List<User> findUsersInCityNative(@Param("city") String city); @Query(value = "SELECT * FROM User WHERE age > :age AND city = :city", nativeQuery = true) List<User> findUsersByAgeAndCityNative(@Param("age") Integer age, @Param("city") String city);
Here are some examples of how you might use these methods in a service class:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getUsersByName(String name) {
return userRepository.findByName(name);
}
public List<User> getUsersByEmails(List<String> emails) {
return userRepository.findByEmailIn(emails);
}
public List<User> getUsersInCity(String city) {
return userRepository.findUsersInCity(city);
}
public User getYoungestUser() {
return userRepository.findFirstByOrderByAgeAsc();
}
public List<User> getUsersByAgeAndCity(int age, String city) {
return userRepository.findUsersByAgeAndCity(age, city);
}
}
Name: Zion Smith Work: Quad22tech's IT helper Supervisor: Mrs.smith Co-workers: Xavier Micah Rishi Emmanuel Dominique Semiera Muhammad Arica Dacion