250x250
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- JSON Web Token
- QGIS
- Maven
- #mojo
- spring
- Linux
- uuid
- RDB
- postgres
- python
- hadoop #hdfs
- postgresql
- LazyInitializationException
- Spring Security
- insert into
- sftp
- posgis
- AuthenticationPrincipal
- Geoserver
- mysql
- JPA
- JWT
- nginx
- Kafka
- psycopg2
- spring boot
- Failed to load ApplicationContext
- Java
- 1093
- Docker
Archives
- Today
- Total
FOREST_CHOI's BLOG
Querydsl의 FetchJoin 과 DTO 본문
728x90
Querydsl을 사용하면서 fetchjoin을 하고 있는데 처음 보는 오류가 발생했다.
Caused by: org.hibernate.QueryException: query specified join fetching,
but the owner of the fetched association was not present in the select list
[FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,
일단 그냥 직관적으로 봤을땐 join할 때 fetch해오면서 어떠한 문제가 생기는 것이었다.
queryFactory.select(Projections.constructor(RelationMemberDto.class,
relation.friend.id.as("friendId"),
relation.friend.name.as("friendName"),
relation.friend.profileImage.as("friendProfileImage")
))
.from(relation)
.join(relation.friend, member).fetchJoin()
.where(
memberIdEq(memberId),
relationTypeEq(condition.getStatus()),
nameEq(condition.getName()),
emailEq(condition.getEmail())
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
이는 처음에 작성하였던 쿼리문이다.
관련해서 문제를 찾아보니, 내가 FetchJoin 자체를 잘못 사용하고 있었다.
FetchJoin을 사용하는 이유는, Entity에서 EntityGraph를 참조하는데 사용하는 것이기 때문에 Entity를 조회해야한다.
DTO를 조회하는 일은 불가능 한 것이었다.
나처럼 Entity를 가져오는 것이 아닌, 별도 DTO로 가져오고 싶다면 FetchJoin이 아닌 그냥 Join을 사용하면 될것이다.
아래는 수정 된 코드이다.
join(relation.friend, member).fetchJoin() -> leftJoin(relation.friend, member) 로 수정하였따.
queryFactory.select(Projections.constructor(RelationMemberDto.class,
relation.friend.id.as("friendId"),
relation.friend.name.as("friendName"),
relation.friend.profileImage.as("friendProfileImage")
))
.from(relation)
.leftJoin(relation.friend, member)
.where(
memberIdEq(memberId),
relationTypeEq(condition.getStatus()),
nameEq(condition.getName()),
emailEq(condition.getEmail())
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();728x90
'프로그래밍 > JPA' 카테고리의 다른 글
| JPA OrderBy rand() Issue (1) | 2022.11.08 |
|---|---|
| @Embedded, @Embeddable (0) | 2022.10.20 |
| JPA? (0) | 2022.10.16 |
| data.sql / insert 구문 시 pk 안들어가는 문제 (0) | 2022.09.23 |
| org.h2.jdbc.JdbcSQLDataException: Hexadecimal string contains non-hex character (0) | 2022.09.20 |
Comments