JPA JPQL vs Querydsl

목차

JPQL

@Test
public void startJPQL(){
// member1 을 찾아라
Member findByJPQL = em.createQuery("select m from Member m where m.username = :username", Member.class)
.setParameter("username", "member1")
.getSingleResult();

assertThat(findByJPQL.getUsername()).isEqualTo("member1");
}

Querydsl

@Test
public void startQuerydsl(){
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
QMember qMember = new QMember("m");

Member findMember = queryFactory
.select(qMember)
.from(qMember)
.where(qMember.username.eq("member1"))
.fetchOne();

assertThat(findMember.getUsername()).isEqualTo("member1");
}
  • Querydsl 은 기본적으로 parameter binding 시 preparedStatement 의 parameter binding 을 사용해 SQL injection 공격으로부터 안전하다.
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=?

jpql 은 런타임 오류가 발생하나 querydsl 은 컴파일시 에러가 발생하게 된다.

Share