From: Gustavo Martin Morcuende Date: Mon, 11 Jul 2016 00:45:46 +0000 (+0200) Subject: Spring JPA: using Querydsl X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=340b232740fb0529997aa84141d7d8771a832106;p=JavaForFun Spring JPA: using Querydsl --- diff --git a/SpringJava/JPA/pom.xml b/SpringJava/JPA/pom.xml index e7137b6..d84e688 100644 --- a/SpringJava/JPA/pom.xml +++ b/SpringJava/JPA/pom.xml @@ -21,6 +21,7 @@ UTF-8 UTF-8 4.3.0.RELEASE + 4.1.3 @@ -218,6 +219,19 @@ 5.2.2.Final + + + com.querydsl + querydsl-apt + ${querydsl.version} + + + com.querydsl + querydsl-jpa + ${querydsl.version} + + + junit @@ -243,12 +257,6 @@ 2.0.11-beta test - - org.springframework - spring-test - ${spring.version} - test - com.h2database h2 @@ -260,16 +268,6 @@ org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.18.1 - - - org.apache.maven.plugins maven-compiler-plugin 3.3 @@ -303,9 +301,49 @@ + + + + com.mysema.maven + apt-maven-plugin + 1.0.4 + + + + process + + + ${project.build.directory}/generated-sources/querydsl + com.querydsl.apt.jpa.JPAAnnotationProcessor + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.11 + + + add-source + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources/querydsl + + + + + + org.apache.maven.plugins maven-surefire-plugin + 2.18.1 **/*IntegrationTest.java @@ -315,6 +353,7 @@ org.apache.maven.plugins maven-failsafe-plugin + 2.18.1 diff --git a/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java b/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java index 49918ad..4b4e6fc 100644 --- a/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java +++ b/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java @@ -2,6 +2,7 @@ package de.spring.example.persistence.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.querydsl.QueryDslPredicateExecutor; import org.springframework.data.repository.PagingAndSortingRepository; import de.spring.example.persistence.domain.Ad; @@ -14,7 +15,7 @@ import de.spring.example.persistence.domain.AdDescription; * Be careful with @Transactional. SimpleJpaRepository has annotated methods. * */ -public interface AdDescriptionRepository extends PagingAndSortingRepository { +public interface AdDescriptionRepository extends PagingAndSortingRepository, QueryDslPredicateExecutor { // Custom Query method (useful when the offered methods by PagingAndSortingRepository are not enough) Page findByAd(Ad ad, Pageable pageable); diff --git a/SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java b/SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java index 8e2f798..bbcf1fa 100644 --- a/SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java +++ b/SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java @@ -1,9 +1,12 @@ package de.spring.example.services; import org.resthub.common.service.CrudService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import de.spring.example.persistence.domain.AdDescription; public interface AdDescriptionService extends CrudService { + public Page queryDslExample(Pageable pageRequest); } diff --git a/SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java b/SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java index ad08ede..6218840 100644 --- a/SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java +++ b/SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java @@ -4,8 +4,13 @@ import javax.inject.Inject; import javax.inject.Named; import org.resthub.common.service.CrudServiceImpl; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +import com.querydsl.core.types.dsl.BooleanExpression; import de.spring.example.persistence.domain.AdDescription; +import de.spring.example.persistence.domain.QAdDescription; import de.spring.example.persistence.repository.AdDescriptionRepository; import de.spring.example.services.AdDescriptionService; @@ -24,4 +29,17 @@ public class AdDescriptionServiceImpl // the JPA repositories and nothing else :) // In this case there is any business logic, but this is just an example. + + + /** + * Using Querydsl. Giving some business logic to this service :) + */ + @Override + public Page queryDslExample(Pageable pageRequest) { + final QAdDescription adDescription = QAdDescription.adDescription1; + final BooleanExpression adDescriptionHasAdLink = adDescription.adLink.contains("gumartinm"); + final BooleanExpression adDescriptionHasDescription = adDescription.adDescription.contains("babucha"); + + return repository.findAll(adDescriptionHasAdLink.and(adDescriptionHasDescription), pageRequest); + } }