SpringDataJPA-Tips
@Query Annotation 등으로 특정 컬럼만 호출하도록 한다.
findById와 같이 일반적인 select에서는 해당 entity의 모든속성을 호출하게 되어있다. 이경우 불필요한 컬럼과 자식entity까지 가져오게 되므로 아래와 같이 처리하는게 좋을 수도 있다.
(기본적인 Projections 관련 사항은 꼭 둘러보자)
기존 findById(id)
Hibernate:
select
company0_.id as id1_0_0_,
company0_.createdAt as createdA2_0_0_,
company0_.createdBy as createdB3_0_0_,
company0_.modifiedAt as modified4_0_0_,
company0_.modifiedBy as modified5_0_0_,
company0_.address as address6_0_0_,
company0_.code as code7_0_0_,
company0_.name as name8_0_0_,
company0_.streetAddress as streetAd9_0_0_
from
company company0_
where
company0_.id=?
New 를 사용한 형태
@Query("select new Company(o.code) from Company o where o.id = :id")
Company findColumnById(@Param("id")Long id);
Hibernate:
select new Company(o.code) from Company o where o.id = :id
Jdbc:
select company0_.code as col_0_0_ from company company0_ where company0_.id=?