티스토리 뷰

spring(boot)

[jpa]query method, projection

수학소년 2023. 10. 11. 00:48

query method 사용시, projection 하기

무지성 dto 만들고, 무지성 query method 만들기

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dto {
	private String id;
}
List<Dto> findByOrderByIdDesc();

WARN [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:207] Resolved [org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [kr.co.oti.pms.api.brd.notice.entity.Notice] to type [kr.co.oti.pms.api.brd.notice.service.BrdNoticeService$Dto]]

 

class가 아니고 interface로 하라는데?

public interface Dto {
	String getId();
}
repository.findByOrderByIdDesc().stream().forEach(each -> {
	log.debug("id: {}", each.getId());
});
List<Dto> findByOrderByIdDesc();

 

projection은 interface로.

'getId' 라는 method명은 Entity의 getter에 의해 정해진 method명으로 쓰는건줄 알았는데,

임의로 바꿔서 해보니 인식을 못함

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [....] on this ManagedType [......]

 

query method가 자동으로 field명을 인식하듯이 이거도 자동으로 인식하나봄.

'spring(boot)' 카테고리의 다른 글

[jpa]테이블 구조에 따라 Entity class  (0) 2023.11.07
[jpa]@OneToOne  (0) 2023.11.04
[security]CORS preflight 요청  (0) 2023.10.09
[jwt]구현 시, 고려할 점들  (0) 2023.09.26
[security]JPA로 구현 시, Role table 없애기  (0) 2023.09.08
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함