Skip to content

Commit 7398d20

Browse files
author
mhewedy
committed
working on #29 Implement FluentQuery introduced in spring data 2.6 to support projection
1 parent 1f1058d commit 7398d20

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.4.2</version>
9+
<version>2.6.1</version> <!-- this version is not inherited by projects using the library, as all spring dependencies are optional -->
1010
<relativePath/> <!-- lookup parent from repository -->
1111
</parent>
1212

src/test/java/com/github/mhewedy/expressions/ExpressionsRepositoryImplTest.java

+42
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.github.mhewedy.expressions.model.*;
5+
import lombok.AllArgsConstructor;
56
import lombok.SneakyThrows;
7+
import lombok.ToString;
68
import lombok.extern.slf4j.Slf4j;
79
import org.junit.jupiter.api.AfterEach;
810
import org.junit.jupiter.api.BeforeEach;
@@ -14,6 +16,7 @@
1416
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
1517
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
1618
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
19+
import org.springframework.data.domain.Example;
1720
import org.springframework.data.domain.Page;
1821
import org.springframework.data.domain.PageRequest;
1922
import org.springframework.data.domain.Sort;
@@ -29,6 +32,7 @@
2932
import java.util.Arrays;
3033
import java.util.List;
3134
import java.util.UUID;
35+
import java.util.stream.Collectors;
3236

3337
import static com.github.mhewedy.expressions.model.Status.ACTIVE;
3438
import static com.github.mhewedy.expressions.model.Status.NOT_ACTIVE;
@@ -588,6 +592,44 @@ public void testHijrahDate_InJava() {
588592
// where e.h_birth_date>=?
589593
}
590594

595+
@Test
596+
public void testFluentQueryInQBE() {
597+
@ToString
598+
@AllArgsConstructor
599+
class EmployeeProjectionDTO {
600+
public final String firstName;
601+
public final String lastName;
602+
}
603+
604+
List<EmployeeProjectionDTO> list = employeeRepository.findBy(Example.of(new Employee()),
605+
query -> query.project("firstName", "lastName")
606+
.stream()
607+
.map(it -> new EmployeeProjectionDTO(it.firstName, it.lastName))
608+
.collect(Collectors.toList())
609+
);
610+
System.out.println(list);
611+
612+
// sql:
613+
/*
614+
SELECT employee0_.id AS id1_2_,
615+
employee0_.created_date AS created_2_2_,
616+
employee0_.active AS active3_2_,
617+
employee0_.age AS age4_2_,
618+
employee0_.birth_date AS birth_da5_2_,
619+
employee0_.department_id AS departm14_2_,
620+
employee0_.first_name AS first_na6_2_,
621+
employee0_.h_birth_date AS h_birth_7_2_,
622+
employee0_.hire_date AS hire_dat8_2_,
623+
employee0_.last_name AS last_nam9_2_,
624+
employee0_.employee_name_ar AS employe10_2_,
625+
employee0_.employee_name_en AS employe11_2_,
626+
employee0_.serial AS serial12_2_,
627+
employee0_.type AS type13_2_
628+
FROM employee employee0_
629+
WHERE ?= 1
630+
*/
631+
}
632+
591633
@SneakyThrows
592634
private String loadResourceJsonFile(String name) {
593635
File file = ResourceUtils.getFile("classpath:" + name + ".json");

0 commit comments

Comments
 (0)