자바생
article thumbnail
순수 JPA Repo + Querydsl, 동적 쿼리
Spring 강의/QueryDSL 2022. 5. 14. 01:54

JPAQueryFactory 사용 Repository에서 JPAQueryFactory를 사용하는 방법은 두가지 있다 Repository에서 직접 EntityManager를 주입 @Repository public class MemberJpaRepository { private final EntityManager em; private final JPAQueryFactory queryFactory; public MemberJpaRepository(EntityManager em) { this.em = em; this.queryFactory = new JPAQueryFactory(em); } } JPAQueryFactory 스프링 빈 등록 @Bean JPAQueryFactory jpaQueryFactory(Ent..

article thumbnail
수정, 삭제 벌크 연산
Spring 강의/QueryDSL 2022. 5. 13. 15:00

벌크 연산이란 쿼리 한번으로 대량의 데이터를 수정하는 것을 말한다 JPQL에서도 지원하여, querydsl도 벌크 연산에 대해 지원한다 수정 벌크 연산 @Test @DisplayName("수정 벌크 연산") void bulkUpdate() throws Exception { long count = queryFactory //영향을 받은 row 수 .update(member) .set(member.username, "비회원") .where(member.age.lt(20)) .execute(); em.flush(); em.clear(); print(); } em.flush(), em.clear()를 해주지 않으면 원하는 결과가 나오지 않게 된다 간단하게 말하면 벌크 연산은 영속성 컨텍스트를 통하지 않고 디비에..

article thumbnail
프로젝션과 결과 반환, 동적 쿼리
Spring 강의/QueryDSL 2022. 5. 12. 15:04

프로젝션? 데이터베이스에서 프로젝션은 조건에 맞는 릴레이션의 속성을 추출한다는 의미이다 따라서 우리는 원하는 속성을 추출하여 얻을 수 있다 만약에 프로젝션 대상이 한개라면 타입을 "명확"하게 정의할 수 있다 하지만 둘 이상이라면 "튜플"이나 "DTO"를 사용해야한다 순수 JPA에서의 DTO 조회 List result = em.createQuery("select new study.querydsl.dto.MemberDto(m.username, m.age) " + "from Member m", MemberDto.class) .getResultList(); 순수 JPA에서 DTO를 조회하기 위해서는 new 명령어를 이용하여 패키지 구조를 다 적어줘야함 또한, 생성자 방식만 지원함 위와 같은 이유로(내 생각엔 패..

article thumbnail
QueryDsl 조인 ~
Spring 강의/QueryDSL 2022. 5. 2. 15:43

기본 조인 List result1 = queryFactory .selectFrom(member) .join(member.team, team) .where(team.name.eq("teamA")) .fetch(); 세타 조인 theta join을 이용해서 연관관계가 없는 필드도 조인을 할 수 있다 sql을 보면 cross가 있는데, "카테시안 곱"을 통하여 모든 엔티티를 조인한 다음, where 조건에 맞는 스키마를 찾는다 그래서 팀 2개, 멤버가 7개 이므로 카테시안 곱의 결과는 총 14개임을 알 수 있다 member, team 테이블 카테시안 곱 결과 결과 on 절 ON 절을 사용하여 "조인 대상 필터링"과 "연관관계가 없는 필드로 외부 조인"할 수 있다 조인 대상 필터링 일반적으로 JPQL에서 조인을..

article thumbnail
JPQL & Querydsl , Querydsl 검색 조건, 결과(fetch), 정렬, 페이징, 집합
Spring 강의/QueryDSL 2022. 4. 29. 22:20

JPQL & Querydsl JPQL @Test @DisplayName("jpql 사용") void startJPQL() throws Exception { Member findMember = em.createQuery("select m from Member m " + "where m.username=:username", Member.class) .setParameter("username", "member1") .getSingleResult(); assertThat(findMember.getUsername()).isEqualTo("member1"); } Querydsl @Test @DisplayName("querydsl 사용") void startQuerydsl() throws Exception{ JPAQ..

728x90

검색 태그