✅ 1. 기본 동작 — Entity 전체 로드
기본적으로 JPA는 Entity 단위로 작동하기 때문에, findAll()이나 findById() 등의 메서드를 호출하면 해당 Entity의 모든 컬럼을 조회합니다.
Employee emp = employeeRepository.findById(1L).get();
위 코드는 DB에서 Employee 엔티티의 모든 컬럼을 SELECT 합니다.
SELECT id, name, department, salary, address, phone FROM employee WHERE id = 1;
✅ 2. 특정 필드만 조회하고 싶을 때 (선택적 조회 방법)
엔티티 전체가 필요하지 않다면, 아래 방법 중 하나로 선택적 필드 조회가 가능합니다.
① JPQL + DTO 생성자 사용
@Query("SELECT new com.example.dto.EmployeeSummary(e.name, e.salary) FROM Employee e WHERE e.department = :dept") List<EmployeeSummary> findEmployeeSummariesByDepartment(@Param("dept") String dept);
👉 DTO 클래스 예시:
public class EmployeeSummary { private String name; private int salary; public EmployeeSummary(String name, int salary) { this.name = name; this.salary = salary; } }
이렇게 하면 SQL은 다음과 같이 실행됩니다:
SELECT name, salary FROM employee WHERE department = ?
즉, 불필요한 컬럼은 조회하지 않음.
② Interface 기반 Projection (Spring Data JPA 기능)
public interface EmployeeNameSalaryView { String getName(); int getSalary(); }
public interface EmployeeRepository extends JpaRepository<Employee, Long> { List<EmployeeNameSalaryView> findByDepartment(String department); }
이 경우 JPA가 내부적으로 필요한 필드만 조회하는 쿼리를 자동 생성합니다.
SELECT name, salary FROM employee WHERE department = ?
③ Native Query 사용
직접 SQL로 필요한 필드만 지정할 수도 있습니다.
@Query( value = "SELECT name, salary FROM employee WHERE department = :dept", nativeQuery = true) List<Map<String, Object>> findEmployeeNameAndSalary(@Param("dept") String dept);
④ EntityGraph (연관 객체 지연로딩 제어용)
만약 연관된 엔티티를 포함해서 선택적으로 가져오고 싶다면 @EntityGraph를 이용할 수도 있습니다.
@EntityGraph(attributePaths = {"department"}) List<Employee> findAll();
🔍 정리
방법특징반환 타입쿼리 생성 방식
| 기본 JPA | 엔티티 전체 조회 | Entity | 자동 |
| JPQL + DTO | 필요한 필드만 | DTO | 수동 지정 |
| Interface Projection | 필요한 필드만 | Interface | 자동 |
| Native Query | 완전 커스텀 | Map, DTO | 직접 SQL 작성 |
| EntityGraph | 연관 엔티티 선택적 조회 | Entity | 자동 |