JAVA/JPA

[Querydsl] fetch 결과 조회

호두밥 2022. 3. 25. 21:32
Method Return Type Description
fetch() List<T> 여러건 조회
fetchOne() T 단건 조회 
결과가 없으면 null, 결과가 여러 건이면 NonUniqueResultException 발생
fetchFirst() T 조회된 여러건 중 첫번째 값을 조회 (limit 1)
결과가 없으면 null 반환
fecthCount() long 조회된 건 수 반환
fetchResults() QueryResults<T> 페이징 정보를 포함한 QueryResults 형태로 반환

fetch

[ example ]

import static study.querydsl.entity.QMember.*;

@PersistenceContext EntityManager em;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);  

List<Member> findMembers = queryFactory
                .selectFrom(member).fetch();

[ query ]

    select
        member0_.member_id as member_i1_1_,
        member0_.age as age2_1_,
        member0_.team_id as team_id4_1_,
        member0_.username as username3_1_ 
    from
        member member0_

 

fetchOne

[ example ]

import static study.querydsl.entity.QMember.*;

@PersistenceContext EntityManager em;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);

    Member memberOne 
    	= queryFactory.selectFrom(member)
        .where(member.username.eq("member1"))
        .fetchOne();

[ query ] 

    select
        member0_.member_id as member_i1_1_,
        member0_.age as age2_1_,
        member0_.team_id as team_id4_1_,
        member0_.username as username3_1_ 
    from
        member member0_ 
    where
        member0_.username='member1';

 

fetchFirst

[ example ]

import static study.querydsl.entity.QMember.*;

@PersistenceContext EntityManager em;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);  

Member memberFirst 
	= queryFactory
    .selectFrom(QMember.member)
    .fetchFirst();

[ query ] 

select 
	member0_.member_id as member_i1_1_,
	member0_.age as age2_1_,
    member0_.team_id as team_id4_1_,
    member0_.username as username3_1_
from member member0_ 
limit 1;

 

 

fetchCount

[ example ]

import static study.querydsl.entity.QMember.*;

@PersistenceContext EntityManager em;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);  

long count = queryFactory.selectFrom(member).fetchCount();

[ query ] 

    select
        count(member0_.member_id) as col_0_0_ 
    from
        member member0_

 

fetchResults

[ example ]

import static study.querydsl.entity.QMember.*;

@PersistenceContext EntityManager em;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);  


// 쿼리 실행
QueryResults<Member> memberQueryResults = queryFactory.selectFrom(member).fetchResults();

// 데이터 출력
System.out.println("memberQueryResults.getLimit() = " + memberQueryResults.getLimit());
System.out.println("memberQueryResults.getOffset() = " + memberQueryResults.getOffset());
System.out.println("memberQueryResults.getTotal() = " + memberQueryResults.getTotal());
memberQueryResults.getResults().forEach(System.out::println);

[ query ]

select
    member0_.member_id as member_i1_1_,
    member0_.age as age2_1_,
    member0_.team_id as team_id4_1_,
    member0_.username as username3_1_ 
from
    member member0_

[출력  console]

memberQueryResults.getLimit() = 9223372036854775807
memberQueryResults.getOffset() = 0
memberQueryResults.getTotal() = 4
Member(id=3, username=member1, age=10)
Member(id=4, username=member2, age=20)
Member(id=5, username=member3, age=30)
Member(id=6, username=member4, age=40)

 

참고

 

'JAVA > JPA' 카테고리의 다른 글

기본키 생성 전략 @GeneratedValue  (0) 2022.02.11