publicinterfaceUsernameOnly {// 조회할 엔티티 필드를 getter 형식으로 지정하면 해당 필드만 선택해서 조회한다.StringgetUsername();}
publicinterfaceMemberRepositoryextendsJpaRepository<Member,Long> {// 메서드 이름은 자유롭게 하면 되고 반환 타입으로 인지한다.List<UsernameOnly> findProjectionsByUsername(String username);}
select member0_.member_id as member_i1_1_, member0_.created_by as created_2_1_, member0_.created_date as created_3_1_, member0_.last_modified_by as last_mod4_1_, member0_.last_modified_date as last_mod5_1_, member0_.age as age6_1_, member0_.team_id as team_id8_1_, member0_.username as username7_1_from member member0_where member0_.username ='m1';
SpEL 문법을 지원한다.
DB에서 엔티티 필드를 다 조회한 다음에 계산하기 때문에 select 절 최적화가 안된다.
classMemberRepositoryTest { @Testpublicvoidprojections() throwsException {...List<UsernameOnly> result =memberRepository.findProjectionsByUsername("m1",UsernameOnly.class); }}
select member0_.username as col_0_0_, team1_.team_id as col_1_0_, team1_.team_id as team_id1_2_, team1_.name as name2_2_from member member0_left outer join team team1_ on member0_.team_id = team1_.team_idwhere member0_.username ='m1';