Spring/Spring Data JPA

[Spring Data JPA] 3-2. JPA Query Methods (3)

noahkim_ 2024. 8. 2. 15:57

7. Other Methods

Custom Implementation

EntityManager
  • Criteria API
public class CustomUserRepositoryImpl implements CustomUserRepository {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<User> findUsersCustomMethod(String criteria) {
        String query = "SELECT u FROM User u WHERE u.criteria = :criteria";
        return entityManager.createQuery(query, User.class)
                            .setParameter("criteria", criteria)
                            .getResultList();
    }
}
public interface UserRepository extends JpaRepository<User, Long>, CustomUserRepository {
    // 기본 JPA 쿼리 메서드
}

 

JdbcTemplate (native SQL)

 

Stored Procedure (DB)

@StoredProcedure
public interface UserRepository extends JpaRepository<User, Long> {
  
  @Procedure(name = "FIND_USERS_BY_STATUS")
  List<User> findUsersByStatus(@Param("status") String status);
}