forked from mariuszs/spring-boot-querydsl
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Employee and Phone example
- Loading branch information
Showing
10 changed files
with
224 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| .idea | ||
| .gradle | ||
| build | ||
| classes | ||
| .idea | ||
| .gradle | ||
| build | ||
| classes | ||
| # Eclipse IDE stuff | ||
| bin/ | ||
| .settings/ | ||
| .classpath | ||
| .project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,18 @@ | ||
| Spring Boot Application with JPA, REST, Audit (Envers), Lombok and QueryDsl | ||
| ----- | ||
|
|
||
| Workarounds | ||
| ---- | ||
|
|
||
| * `package-info.java` is required, for `Revision.java` to compile. | ||
| Spring Boot Application with JPA, REST, Audit (Envers), Lombok and QueryDsl | ||
| ----- | ||
|
|
||
| Employee and Phone example is taken from [Java Persistence wikibook](https://en.wikibooks.org/wiki/Java_Persistence). | ||
|
|
||
|  | ||
|
|
||
|
|
||
| Workarounds | ||
| ---- | ||
|
|
||
| * `package-info.java` is required, for `Revision.java` to compile. | ||
|
|
||
| ## Links | ||
|
|
||
| - [Hibernate ORM Envers](http://hibernate.org/orm/envers/), [docs](http://docs.jboss.org/envers/docs/index.html) | ||
| - https://en.wikibooks.org/wiki/Java_Persistence | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.example; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import com.example.domain.Employee; | ||
| import com.example.domain.Phone; | ||
| import com.example.repositories.EmployeeRepository; | ||
| import com.example.repositories.PhoneRepository; | ||
| import com.example.servicies.EmployeeService; | ||
|
|
||
| /** | ||
| * Beans for manual testing: uncomment @Configuration or specific @Bean to enable, | ||
| * then Spring Boot will execute on start. | ||
| * @author Paul Verest | ||
| */ | ||
| @Configuration // uncomment @Configuration annotation to enable | ||
| public class SpringBootQuerydslTestBean { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(SpringBootQuerydslTestBean.class); | ||
|
|
||
| @Bean // uncomment @Bean annotation to enable | ||
| public CommandLineRunner testQuerySQLfinds(final EmployeeRepository repository, | ||
| final PhoneRepository phoneRepository, final EmployeeService service) { | ||
| return new CommandLineRunner() { | ||
| @Override | ||
| public void run(String... args) { | ||
| log.info("Creating test case 1 data..."); | ||
| Employee emp1 = new Employee(); | ||
| emp1.setFirstName("Antony"); | ||
| emp1.setLastName("Long"); | ||
| repository.save(emp1); | ||
|
|
||
| Phone tel1 = new Phone("123456789"); | ||
| tel1.setOwner(emp1); | ||
| phoneRepository.save(tel1); | ||
| Phone tel2 = new Phone("+20123456789"); | ||
| tel2.setOwner(emp1); | ||
| phoneRepository.save(tel2); | ||
|
|
||
| List<Phone> antonyPhones = phoneRepository.findByOwner(emp1); | ||
| log.info("Phone owned by "+emp1+":"); | ||
| for (Phone p : antonyPhones) { | ||
| log.info(p.toString()); | ||
| } | ||
| log.info(""); | ||
|
|
||
| Iterable<Employee> employees = service.findEmployeesByPhoneNumber("456"); | ||
| log.info("Employees found with findEmployeesByPhoneNumber('456'):"); | ||
| for (Employee e : employees) { | ||
| log.info(e.toString()); | ||
| } | ||
| log.info(""); | ||
|
|
||
| Iterable<Employee> employees2 = service.findEmployeesByPhoneNumber("123456789"); | ||
| log.info("Employees found with findEmployeesByPhoneNumber('123456789'):"); | ||
| for (Employee e : employees2) { | ||
| log.info(e.toString()); | ||
| } | ||
| log.info(""); | ||
|
|
||
| } | ||
| }; | ||
| } | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.example.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.OneToMany; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Entity | ||
| @Data | ||
| public class Employee { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| private Long id; | ||
|
|
||
| private String firstName; | ||
|
|
||
| private String lastName; | ||
|
|
||
| @OneToMany(mappedBy="owner") | ||
| private List<Phone> phones; | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.example.domain; | ||
|
|
||
| import javax.persistence.Entity; | ||
| import javax.persistence.FetchType; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.JoinColumn; | ||
| import javax.persistence.ManyToOne; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Entity | ||
| @Data | ||
| public class Phone { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| private Long id; | ||
|
|
||
| private String number; | ||
|
|
||
| public Phone(){//for JPA | ||
| } | ||
| public Phone(String number){ | ||
| this.number = number; | ||
| } | ||
|
|
||
| @ManyToOne(fetch=FetchType.LAZY) | ||
| //@JoinColumn(name="OWNER_ID") | ||
| private Employee owner; | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/repositories/EmployeeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.repositories; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.querydsl.QueryDslPredicateExecutor; | ||
|
|
||
| import com.example.domain.Employee; | ||
|
|
||
| public interface EmployeeRepository extends JpaRepository<Employee, Long>, QueryDslPredicateExecutor<Employee> { | ||
|
|
||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/repositories/PhoneRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.example.repositories; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.querydsl.QueryDslPredicateExecutor; | ||
|
|
||
| import com.example.domain.Employee; | ||
| import com.example.domain.Phone; | ||
|
|
||
| public interface PhoneRepository extends JpaRepository<Phone, Long>, QueryDslPredicateExecutor<Phone> { | ||
|
|
||
| List<Phone> findByOwner(Employee owner); | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.example.servicies; | ||
|
|
||
| import com.example.domain.Employee; | ||
|
|
||
| public interface EmployeeService { | ||
|
|
||
| Iterable<Employee> findEmployeesByPhoneNumber(String phoneNumber); | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/example/servicies/EmployeeServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.example.servicies; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import com.example.domain.Employee; | ||
| import com.example.repositories.EmployeeRepository; | ||
|
|
||
| import static com.example.domain.QEmployee.employee; | ||
|
|
||
| @Service | ||
| public class EmployeeServiceImpl implements EmployeeService{ | ||
|
|
||
| @Autowired | ||
| EmployeeRepository repository; | ||
|
|
||
| @Override | ||
| public Iterable<Employee> findEmployeesByPhoneNumber(String phoneNumber) { | ||
| return repository.findAll(employee.phones.any().number.like(phoneNumber)); | ||
| } | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.example.web; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
|
|
||
| import com.example.servicies.EmployeeService; | ||
|
|
||
| public class EmployeeController { | ||
|
|
||
| @Autowired | ||
| EmployeeService service; | ||
|
|
||
| } |