JPA 에서 QueryDSL 사용하기위해 JPAQuery 인스턴스생성방법 http://ojc.asia, http://ojcedu.com 1. JPAQuery 를직접생성하기 JPAQuery 인스턴스생성하기 QueryDSL의 JPAQuery API를사용하려면 JPAQuery 인스턴스를생성하면된다. // entitymanager는 JPA의 EntityManage JPAQuery<?> query = new JPAQuery<Void>(entityManager); 2. JPAQueryFactory 를이용한 JPAQuery 인스턴스생성 QueryDSL 을이용하여 JPA 에서쿼리를사용한다면 JPAQueryFactory 를이용하면되는데, JPAQueryFactory 는 JPQLQueryFactory 인터페이스를구현했으며, JPAQuery 인스턴스를포함하 여다양한방법으로쿼리할수있다. JPAQueryFactory 의모습은아래와같다. 생성할때 EntityManager 만인자로넣어생성할수 도있고, JPQLTemplate 도같이인자로줘서생성할수있다. package com.querydsl.jpa.impl; import javax.annotation.nullable; import javax.inject.provider; import javax.persistence.entitymanager; import com.querydsl.core.tuple; import com.querydsl.core.types.entitypath; import com.querydsl.core.types.expression; import com.querydsl.core.types.dsl.expressions; import com.querydsl.jpa.jpqlqueryfactory; import com.querydsl.jpa.jpqltemplates; /**
* Factory class for query and DML clause creation * * @author tiwe * */ public class JPAQueryFactory implements JPQLQueryFactory { @Nullable private final JPQLTemplates templates; private final Provider<EntityManager> entitymanager; public JPAQueryFactory(final EntityManager entitymanager) { this.entitymanager = new Provider<EntityManager>() { public EntityManager get() { return entitymanager; ; this.templates = null; public JPAQueryFactory(JPQLTemplates templates, final EntityManager entitymanager) { this.entitymanager = new Provider<EntityManager>() { public EntityManager get() { return entitymanager; ; this.templates = templates; public JPAQueryFactory(Provider<EntityManager> entitymanager) { this.entitymanager = entitymanager; this.templates = null; public JPAQueryFactory(JPQLTemplates templates, Provider<EntityManager> entitymanager) { this.entitymanager = entitymanager;
this.templates = templates; public JPADeleteClause delete(entitypath<?> path) { if (templates!= null) { return new JPADeleteClause(entityManager.get(), path, templates); else { return new JPADeleteClause(entityManager.get(), path); public <T> JPAQuery<T> select(expression<t> expr) { return query().select(expr); public JPAQuery<Tuple> select(expression<?>... exprs) { return query().select(exprs); public <T> JPAQuery<T> selectdistinct(expression<t> expr) { return select(expr).distinct(); public JPAQuery<Tuple> selectdistinct(expression<?>... exprs) { return select(exprs).distinct(); public JPAQuery<Integer> selectone() { return select(expressions.one); public JPAQuery<Integer> selectzero() {
return select(expressions.zero); public <T> JPAQuery<T> selectfrom(entitypath<t> from) { return select(from).from(from); public JPAQuery<?> from(entitypath<?> from) { return query().from(from); public JPAQuery<?> from(entitypath<?>... from) { return query().from(from); public JPAUpdateClause update(entitypath<?> path) { if (templates!= null) { return new JPAUpdateClause(entityManager.get(), path, templates); else { return new JPAUpdateClause(entityManager.get(), path); public JPAQuery<?> query() { if (templates!= null) { return new JPAQuery<Void>(entityManager.get(), templates); else { return new JPAQuery<Void>(entityManager.get()); 스프링설정파일에서 @Bean 으로빈을등록해야하는데스프링부트인경우메인에서다음
과같이빈으로등록하면된다. import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.bean; import com.querydsl.jpa.jpqltemplates; import com.querydsl.jpa.impl.jpaqueryfactory; @SpringBootApplication public class JpaqueryfactoryExamApplication { public static void main(string[] args) { SpringApplication.run(JpaqueryfactoryExamApplication.class, args); @PersistenceContext EntityManager em; @Bean public JPAQueryFactory queryfactory() { //return new JPAQueryFactory(JPQLTemplates.DEFAULT, em); return new JPAQueryFactory(em); Repository 구현체에서는다음과같이빈을주입받아사용하면된다. @Autowired JPAQueryFactory queryfactory; /* Emp 테이블에서 job 을조건으로이름내림차순으로검색 */ public List<Emp> selectbyjoborderbyenamedesc(string job) { List<Emp> emps = queryfactory.selectfrom(emp).where(emp.job.eq( job)).orderby(emp.ename.desc()).fetch(); return emps;