Spring JPA: using Querydsl
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 11 Jul 2016 00:45:46 +0000 (02:45 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 11 Jul 2016 00:52:21 +0000 (02:52 +0200)
SpringJava/JPA/pom.xml
SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java
SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java
SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java

index e7137b6..d84e688 100644 (file)
@@ -21,6 +21,7 @@
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
                <spring.version>4.3.0.RELEASE</spring.version>
+               <querydsl.version>4.1.3</querydsl.version>
        </properties>
 
        <profiles>
                        <version>5.2.2.Final</version>
                </dependency>
 
+               <!-- Using Querydsl -->
+               <dependency>
+                       <groupId>com.querydsl</groupId>
+                       <artifactId>querydsl-apt</artifactId>
+                       <version>${querydsl.version}</version>
+               </dependency>
+               <dependency>
+                       <groupId>com.querydsl</groupId>
+                       <artifactId>querydsl-jpa</artifactId>
+                       <version>${querydsl.version}</version>
+               </dependency>
+
+
                <!-- Unitary and integration tests -->
                <dependency>
                        <groupId>junit</groupId>
                        <version>2.0.11-beta</version>
                        <scope>test</scope>
                </dependency>
-               <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-test</artifactId>
-            <version>${spring.version}</version>
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>com.h2database</groupId>
             <artifactId>h2</artifactId>
                <plugins>
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
-                               <artifactId>maven-surefire-plugin</artifactId>
-                               <version>2.18.1</version>
-                       </plugin>
-                       <plugin>
-                               <groupId>org.apache.maven.plugins</groupId>
-                               <artifactId>maven-failsafe-plugin</artifactId>
-                               <version>2.18.1</version>
-                       </plugin>
-                       <plugin>
-                               <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
                                <version>3.3</version>
                                <configuration>
                                        </archive>
                                </configuration>
                        </plugin>
+                       
+                       <!-- Using Querydsl -->
+                       <plugin>
+                         <groupId>com.mysema.maven</groupId>
+                         <artifactId>apt-maven-plugin</artifactId>
+                         <version>1.0.4</version>
+                         <executions>
+                           <execution>
+                             <goals>
+                               <goal>process</goal>
+                             </goals>
+                             <configuration>
+                               <outputDirectory>${project.build.directory}/generated-sources/querydsl</outputDirectory>
+                               <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
+                             </configuration>
+                           </execution>
+                         </executions>
+                       </plugin>
+                       <!-- Required in order to work with m2e plugin for Eclipse  -->
+               <plugin>
+                   <groupId>org.codehaus.mojo</groupId>
+                   <artifactId>build-helper-maven-plugin</artifactId>
+                   <version>1.11</version>
+                   <executions>
+                       <execution>
+                           <id>add-source</id>
+                           <phase>generate-sources</phase>
+                           <goals>
+                               <goal>add-source</goal>
+                           </goals>
+                           <configuration>
+                               <sources>
+                                   <source>${project.build.directory}/generated-sources/querydsl</source>
+                               </sources>
+                           </configuration>
+                       </execution>
+                   </executions>
+               </plugin>
+                       
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-plugin</artifactId>
+                               <version>2.18.1</version>
                                <configuration>
                                        <excludes>
                                                <exclude>**/*IntegrationTest.java</exclude>
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-failsafe-plugin</artifactId>
+                               <version>2.18.1</version>
                                <executions>
                                        <execution>
                                                <goals>
index 49918ad..4b4e6fc 100644 (file)
@@ -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 <code>@Transactional</code>. SimpleJpaRepository has annotated methods.
  *
  */
-public interface AdDescriptionRepository extends PagingAndSortingRepository<AdDescription, Long> {
+public interface AdDescriptionRepository extends PagingAndSortingRepository<AdDescription, Long>, QueryDslPredicateExecutor<AdDescription> {
 
        // Custom Query method (useful when the offered methods by PagingAndSortingRepository are not enough)
        Page<AdDescription> findByAd(Ad ad, Pageable pageable);
index 8e2f798..bbcf1fa 100644 (file)
@@ -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<AdDescription, Long> {
 
+       public Page<AdDescription> queryDslExample(Pageable pageRequest);
 }
index ad08ede..6218840 100644 (file)
@@ -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<AdDescription> 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);
+       }
 }