티스토리 뷰

Programming/JPA

Query DSL, Criteria 설정

Albothyl 2019. 3. 1. 14:26

1. Dependencies

  • compile "org.springframework:spring-orm:4.3.11.RELEASE"
  • compile "org.springframework.data:spring-data-jpa:1.11.7.RELEASE"
  • compile "org.hibernate:hibernate-core:5.2.11.Final"
  • compile "org.hibernate:hibernate-entitymanager:5.2.11.Final"
  • compile "com.querydsl:querydsl-jpa:4.1.3"

2. Plugin setting

  • Maven
  • <project>

      <build>

      <plugins>

        

        //QueryDSL

        <plugin>

          <groupId>com.mysema.maven</groupId>

          <artifactId>apt-maven-plugin</artifactId>

          <version>1.1.3</version>

          <executions>

            <execution>

              <goals>

                <goal>process</goal>

              </goals>

              <configuration>

                <outputDirectory>target/generated-sources/java</outputDirectory>

                <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>

              </configuration>

            </execution>

          </executions>

        </plugin>


        //Criteria

        <plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-compiler-plugin</artifactId>

          <version>3.7.0</version>

          <configuration>

            <source>1.8</source>

            <target>1.8</target>

            <encoding>UTF-8</encoding>

            <compilerArguments>

              <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>

            </compilerArguments>

          </configuration>

        </plugin>


      </plugins>

      </build>

    </project>

  • Gradle
  • repositories {

    mavenCentral()

    }


    afterEvaluate {

    def processors = new LinkedHashSet()


    configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { raf ->

    def cid = raf.getModuleVersion().getId()


    if (cid.group.equals('com.querydsl') && cid.name.equals('querydsl-core')) {

    def queryDslAptVersion = cid.version

    logger.info "[jpaModelGen-${project.name}] querydsl-apt version ${queryDslAptVersion}"

    dependencies {

    jpaMetamodelGen "com.querydsl:querydsl-apt:${queryDslAptVersion}"

    }


    processors << 'com.querydsl.apt.jpa.JPAAnnotationProcessor'

    }


    if (cid.group.equals('org.hibernate') && cid.name.equals('hibernate-core')) {

    def hibernateJpaModelGenVersion = cid.version

    dependencies {

    jpaMetamodelGen "org.hibernate:hibernate-jpamodelgen:${hibernateJpaModelGenVersion}"

    }


    processors << 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor'

    }

    }

    }


    sourceSets {

    main {

    java {

    srcDir 'src/generated/java-jpa'

    }

    }

    }

3. How to use

  • QueryDSL
    1. Repositroy에 "QueryDslPredicateExecutor"와 "CustomRepository"를 상속받는다.
    2. CustomRepository의 구현체 RepositoryImpl을 생성한다.
    3. 사용할 Query를 작성한다.
  • Criteria
    1. Repository에 "JpaSpecificationExecutor"를 상속 받는다.
    2. Criteria (specification)을 작성한다.

4. Example

QueryDSL

@Repository
public class CardRepositoryImpl implements CardRepositoryCustom {

@PersistenceContext(unitName = "kanban")
private EntityManager entityManager;

@Override
public List<Card> search(String title, CardStatus status, String color) {

JPAQuery jpaQuery = new JPAQuery(entityManager);
QCard card = QCard.card;

jpaQuery.from(card)
.where(card.title.like("%" + title + "%"))
.where(card.status.eq(status))
.where(card.color.eq(color))
.orderBy(card.id.desc());

return jpaQuery.fetch();
}
}

Criteria

public class QuerySpec<T> {
public Specification<T> equal(SingularAttribute<T, String> attribute, String value) {
if (StringUtils.isBlank(value)) {
return null;
}

return (root, query, builder) -> builder.equal(root.get(attribute), value);
}
}


** 참고하면 좋은 블로그

  • https://jojoldu.tistory.com/372
  • http://kwonnam.pe.kr/wiki/gradle/jpa_metamodel_generation


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

Embedded Mapping  (0) 2019.05.29
Discriminate Mapping  (0) 2019.05.26
Query DSL  (0) 2018.08.05
Hibernate Criteria  (0) 2018.08.05
Hibernate @Version  (0) 2018.07.16
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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 31
글 보관함