Spring Data JPA @Query 사용하고 있는데 반환값을 DTO로 매핑하고 싶을때
@Query(value = "SELECT new com.example.NamesOnly(u.firstname, u.lastname) from User u")
List<NamesOnly> findAll();
SELECT문에 생성자 표현식으로 작성하면된다.
It is important to note that Class-based projections with JPQL is limited to constructor expressions in your JPQL expression, e.g. SELECT new com.example.NamesOnly(u.firstname, u.lastname) from User u. (Note the usage of a FQDN for the DTO type!) This JPQL expression can be used in @Query annotations as well where you define any named queries. And it’s important to point out that class-based projections do not work with native queries AT ALL. As a workaround you may use named queries with ResultSetMapping or the Hibernate-specific ResultListTransformer
출처 : https://docs.spring.io/spring-data/jpa/reference/repositories/projections.html#projection.dynamic
Spring cache SpEL 표현식 메서드 인자 이름으로 사용할 수 없을떄
@Cacheable(value = "123", key = "#p0.getName()")
@Override
public String test(RequestDto testRequestDto) {
return testRequestDto.getName();
}
#testRequestDto 대신 #p0을 사용하면 된다.
The result of the method call (the value to be cached). Only available in unless expressions, cache put expressions (to compute the key), or cache evict expressions (when beforeInvocation is false). For supported wrappers (such as Optional), #result refers to the actual object, not the wrapper.
'Others' 카테고리의 다른 글
| Redis, Prometheus, Grafana 모니터링 (0) | 2025.10.31 |
|---|---|
| mybatis sql 로그 pretty하게 남기기 (4) | 2025.07.31 |
| Redis + Sentine (0) | 2025.03.28 |
| 개발자 커뮤니티 QNA 답변하면서 공부/찾은 내용(#3) (0) | 2024.10.10 |
| 개발자 커뮤니티 QNA 답변하면서 공부/찾은 내용(#2) (0) | 2024.09.09 |