├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main ├── java │ └── com │ │ └── example │ │ ├── SpringBootQuerydslApplication.java │ │ ├── SpringBootQuerydslTestBean.java │ │ ├── Token.java │ │ ├── TokenController.java │ │ ├── TokenRepository.java │ │ ├── audit │ │ ├── Revision.java │ │ └── package-info.java │ │ ├── domain │ │ ├── Employee.java │ │ └── Phone.java │ │ ├── repositories │ │ ├── EmployeeRepository.java │ │ └── PhoneRepository.java │ │ ├── servicies │ │ ├── EmployeeService.java │ │ └── EmployeeServiceImpl.java │ │ └── web │ │ └── EmployeeController.java └── resources │ └── application.yaml └── test └── java └── com └── example └── SpringBootQuerydslApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .gradle 3 | build 4 | classes 5 | # Eclipse IDE stuff 6 | bin/ 7 | .settings/ 8 | .classpath 9 | .project 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring Boot Application with JPA, REST, Audit (Envers), Lombok and QueryDsl 2 | ----- 3 | 4 | Employee and Phone example is taken from [Java Persistence wikibook](https://en.wikibooks.org/wiki/Java_Persistence). 5 | 6 | ![](https://upload.wikimedia.org/wikipedia/commons/7/7e/ObjectRelational-ManyToOne2.jpg) 7 | 8 | Executed query 9 | 10 | ```java 11 | public Iterable findEmployeesByPhoneNumber(String phoneNumber) { 12 | return repository.findAll(employee.phones.any().number.contains(phoneNumber)); 13 | } 14 | ``` 15 | 16 | IntelliJ Idea configuration: 17 | 18 | * Go to Preferences -> Build, Execution, Deployment -> Annotation Processors; 19 | * Check Enable annotation processing checkbox; 20 | * In "Store generated sources relative to:" select Module content root. 21 | 22 | Workarounds 23 | ---- 24 | 25 | * `package-info.java` is required, for `Revision.java` to compile. 26 | 27 | ## Links 28 | 29 | - [Spring projects links to docs and examples summary](https://github.com/paulvi/spring-projects-links-to-docs-and-examples-summary) 30 | - [Hibernate ORM Envers](http://hibernate.org/orm/envers/), [docs](http://docs.jboss.org/envers/docs/index.html) 31 | - https://en.wikibooks.org/wiki/Java_Persistence 32 | 33 | - http://bsideup.blogspot.com/2015/04/querydsl-with-gradle-and-idea.html -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '1.3.5.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | maven { 8 | url "https://plugins.gradle.org/m2/" 9 | } 10 | } 11 | dependencies { 12 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 13 | } 14 | } 15 | 16 | apply plugin: 'java' 17 | apply plugin: 'eclipse' 18 | apply plugin: 'spring-boot' 19 | apply plugin: 'idea' 20 | 21 | jar { 22 | baseName = 'spring-boot-querydsl' 23 | version = '0.0.1-SNAPSHOT' 24 | } 25 | sourceCompatibility = 1.8 26 | targetCompatibility = 1.8 27 | 28 | ext { 29 | queryDslVersion = '3.6.3' 30 | } 31 | 32 | compileJava { 33 | options.compilerArgs += [ 34 | '-parameters', 35 | '-Aquerydsl.excludedPackages=com.example.bad' 36 | ] 37 | } 38 | 39 | idea { 40 | module { 41 | sourceDirs += file('generated/') 42 | } 43 | } 44 | 45 | repositories { 46 | mavenCentral() 47 | } 48 | 49 | dependencies { 50 | compile('org.springframework.boot:spring-boot-starter-data-jpa') 51 | compile('org.springframework.boot:spring-boot-starter-web') 52 | compile('org.hibernate:hibernate-envers') 53 | compile('org.springframework.boot:spring-boot-starter-hateoas') 54 | compile('org.yaml:snakeyaml') 55 | compile "com.mysema.querydsl:querydsl-jpa:$queryDslVersion" 56 | 57 | runtime 'com.h2database:h2' 58 | 59 | compileOnly "com.mysema.querydsl:querydsl-apt:$queryDslVersion:jpa" 60 | compileOnly 'org.projectlombok:lombok:1.16.8' 61 | 62 | testCompile('org.springframework.boot:spring-boot-starter-test') 63 | } 64 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariuszs/spring-boot-querydsl/8e0c89e69fec1b5a48af86f165de34788291765f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jun 20 15:10:51 CEST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/java/com/example/SpringBootQuerydslApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootQuerydslApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootQuerydslApplication.class, args); 11 | } 12 | 13 | public void foo(){ 14 | 15 | 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/example/SpringBootQuerydslTestBean.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.util.List; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.boot.CommandLineRunner; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | import com.example.domain.Employee; 12 | import com.example.domain.Phone; 13 | import com.example.repositories.EmployeeRepository; 14 | import com.example.repositories.PhoneRepository; 15 | import com.example.servicies.EmployeeService; 16 | 17 | /** 18 | * Beans for manual testing: uncomment @Configuration or specific @Bean to enable, 19 | * then Spring Boot will execute on start. 20 | * @author Paul Verest 21 | */ 22 | @Configuration // uncomment @Configuration annotation to enable 23 | public class SpringBootQuerydslTestBean { 24 | 25 | private static final Logger log = LoggerFactory.getLogger(SpringBootQuerydslTestBean.class); 26 | 27 | @Bean // uncomment @Bean annotation to enable 28 | public CommandLineRunner testQuerySQLfinds(final EmployeeRepository repository, 29 | final PhoneRepository phoneRepository, final EmployeeService service) { 30 | return new CommandLineRunner() { 31 | @Override 32 | public void run(String... args) { 33 | log.info("Creating test case 1 data..."); 34 | Employee emp1 = new Employee(); 35 | emp1.setFirstName("Antony"); 36 | emp1.setLastName("Long"); 37 | repository.save(emp1); 38 | 39 | Phone tel1 = new Phone("123456789"); 40 | tel1.setOwner(emp1); 41 | phoneRepository.save(tel1); 42 | Phone tel2 = new Phone("+20123456789"); 43 | tel2.setOwner(emp1); 44 | phoneRepository.save(tel2); 45 | 46 | List antonyPhones = phoneRepository.findByOwner(emp1); 47 | log.info("Phone owned by "+emp1+":"); 48 | for (Phone p : antonyPhones) { 49 | log.info(p.toString()); 50 | } 51 | log.info(""); 52 | 53 | Iterable employees = service.findEmployeesByPhoneNumber("456"); 54 | log.info("Employees found with findEmployeesByPhoneNumber('456'):"); 55 | for (Employee e : employees) { 56 | log.info(e.toString()); 57 | } 58 | log.info(""); 59 | 60 | Iterable employees2 = service.findEmployeesByPhoneNumber("123456789"); 61 | log.info("Employees found with findEmployeesByPhoneNumber('123456789'):"); 62 | for (Employee e : employees2) { 63 | log.info(e.toString()); 64 | } 65 | log.info(""); 66 | 67 | } 68 | }; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/example/Token.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import lombok.Data; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.Id; 8 | 9 | @Entity 10 | @Data 11 | public class Token { 12 | 13 | @Id 14 | @GeneratedValue 15 | private Long id; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/example/TokenController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import static com.example.QToken.token; 8 | 9 | @RestController 10 | public class TokenController { 11 | 12 | private final TokenRepository tokenRepository; 13 | 14 | @Autowired 15 | public TokenController(TokenRepository tokenRepository) { 16 | this.tokenRepository = tokenRepository; 17 | } 18 | 19 | @RequestMapping("/") 20 | public void root() { 21 | 22 | tokenRepository.findAll(token.id.eq(1L)); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/example/TokenRepository.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.querydsl.QueryDslPredicateExecutor; 5 | 6 | interface TokenRepository extends JpaRepository, QueryDslPredicateExecutor { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/example/audit/Revision.java: -------------------------------------------------------------------------------- 1 | package com.example.audit; 2 | 3 | import org.hibernate.envers.DefaultRevisionEntity; 4 | import org.hibernate.envers.RevisionEntity; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.Table; 8 | 9 | @Entity 10 | @RevisionEntity 11 | @Table(name = "audit_revisions") 12 | public class Revision extends DefaultRevisionEntity { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/example/audit/package-info.java: -------------------------------------------------------------------------------- 1 | @QueryEntities({DefaultRevisionEntity.class}) 2 | package com.example.audit; 3 | 4 | import com.mysema.query.annotations.QueryEntities; 5 | import org.hibernate.envers.DefaultRevisionEntity; 6 | -------------------------------------------------------------------------------- /src/main/java/com/example/domain/Employee.java: -------------------------------------------------------------------------------- 1 | package com.example.domain; 2 | 3 | import java.util.List; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.FetchType; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.Id; 9 | import javax.persistence.OneToMany; 10 | 11 | import lombok.Data; 12 | 13 | @Entity 14 | @Data 15 | public class Employee { 16 | 17 | @Id 18 | @GeneratedValue 19 | private Long id; 20 | 21 | private String firstName; 22 | 23 | private String lastName; 24 | 25 | @OneToMany(mappedBy="owner", fetch=FetchType.LAZY) 26 | private List phones; 27 | 28 | @Override 29 | public String toString() { 30 | return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/example/domain/Phone.java: -------------------------------------------------------------------------------- 1 | package com.example.domain; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.FetchType; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | import javax.persistence.JoinColumn; 8 | import javax.persistence.ManyToOne; 9 | 10 | import lombok.Data; 11 | 12 | @Entity 13 | @Data 14 | public class Phone { 15 | 16 | @Id 17 | @GeneratedValue 18 | private Long id; 19 | 20 | private String number; 21 | 22 | public Phone(){//for JPA 23 | } 24 | public Phone(String number){ 25 | this.number = number; 26 | } 27 | 28 | @ManyToOne(fetch=FetchType.EAGER) 29 | //@JoinColumn(name="OWNER_ID") 30 | private Employee owner; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/example/repositories/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.repositories; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.querydsl.QueryDslPredicateExecutor; 5 | 6 | import com.example.domain.Employee; 7 | 8 | public interface EmployeeRepository extends JpaRepository, QueryDslPredicateExecutor { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/example/repositories/PhoneRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.repositories; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.querydsl.QueryDslPredicateExecutor; 7 | 8 | import com.example.domain.Employee; 9 | import com.example.domain.Phone; 10 | 11 | public interface PhoneRepository extends JpaRepository, QueryDslPredicateExecutor { 12 | 13 | List findByOwner(Employee owner); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/example/servicies/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.example.servicies; 2 | 3 | import com.example.domain.Employee; 4 | 5 | public interface EmployeeService { 6 | 7 | Iterable findEmployeesByPhoneNumber(String phoneNumber); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/example/servicies/EmployeeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.servicies; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.example.domain.Employee; 7 | import com.example.repositories.EmployeeRepository; 8 | 9 | import static com.example.domain.QEmployee.employee; 10 | 11 | @Service 12 | public class EmployeeServiceImpl implements EmployeeService{ 13 | 14 | @Autowired 15 | EmployeeRepository repository; 16 | 17 | @Override 18 | public Iterable findEmployeesByPhoneNumber(String phoneNumber) { 19 | return repository.findAll(employee.phones.any().number.contains(phoneNumber)); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/example/web/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package com.example.web; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | 5 | import com.example.servicies.EmployeeService; 6 | 7 | public class EmployeeController { 8 | 9 | @Autowired 10 | EmployeeService service; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8020 3 | spring: 4 | h2: 5 | console: 6 | enabled: true 7 | jpa: 8 | database: H2 9 | show-sql: true 10 | hibernate: 11 | format_sql: true 12 | ddl-auto: create 13 | naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy -------------------------------------------------------------------------------- /src/test/java/com/example/SpringBootQuerydslApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.SpringApplicationConfiguration; 6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 | 8 | @RunWith(SpringJUnit4ClassRunner.class) 9 | @SpringApplicationConfiguration(classes = SpringBootQuerydslApplication.class) 10 | public class SpringBootQuerydslApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------