/
DepartmentApplication.java
201 lines (181 loc) · 9.23 KB
/
DepartmentApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package io.github.bartoszpop.jpa.specification.example;
import io.github.bartoszpop.jpa.specification.ExpressionSpecifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.persistence.criteria.Path;
import java.time.LocalDate;
import java.util.List;
import static io.github.bartoszpop.jpa.specification.CompositeSpecification.not;
import static io.github.bartoszpop.jpa.specification.ExpressionSpecifications.greaterThanOrEqualTo;
import static io.github.bartoszpop.jpa.specification.ExpressionSpecifications.in;
import static io.github.bartoszpop.jpa.specification.ExpressionSpecifications.lessThan;
import static io.github.bartoszpop.jpa.specification.example.DepartmentMatchers.department;
import static io.github.bartoszpop.jpa.specification.example.DepartmentSpecifications.fetchEmployees;
import static io.github.bartoszpop.jpa.specification.example.DepartmentSpecifications.joinEmployees;
import static io.github.bartoszpop.jpa.specification.example.DepartmentSpecifications.name;
import static io.github.bartoszpop.jpa.specification.example.EmployeeMatchers.employee;
import static io.github.bartoszpop.jpa.specification.example.EmployeeSpecifications.dateOfBirth;
import static io.github.bartoszpop.jpa.specification.example.EmployeeSpecifications.firstName;
import static io.github.bartoszpop.jpa.specification.example.EmployeeSpecifications.secondName;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
@SpringBootApplication
public class DepartmentApplication {
public static void main(String[] args) {
SpringApplication.run(DepartmentApplication.class, args);
}
@Autowired
public void friendsCompany(DepartmentRepository departmentRepository) {
var chandler = new Employee("Chandler", "Bing", LocalDate.of(1968, 4, 8));
var dina = new Employee("Dina", "Tribbiani", LocalDate.of(1980, 12, 19));
var joey = new Employee("Joey", "Tribbiani", LocalDate.of(1968, 1, 9));
var monica = new Employee("Monica", "Geller", LocalDate.of(1970, 3, 22));
var phoebe = new Employee("Phoebe", "Buffay", LocalDate.of(1967, 2, 16));
var rachel = new Employee("Rachel", "Green", LocalDate.of(1969, 5, 5));
var ross = new Employee("Ross", "Geller", LocalDate.of(1967, 10, 18));
var salesDepartment = new Department("Sales");
salesDepartment.addEmployee(chandler);
salesDepartment.addEmployee(joey);
salesDepartment.addEmployee(monica);
salesDepartment.addEmployee(phoebe);
salesDepartment.addEmployee(ross);
departmentRepository.save(salesDepartment);
var financeDepartment = new Department("Finance");
financeDepartment.addEmployee(rachel);
departmentRepository.save(financeDepartment);
var hrDepartment = new Department("Human Resources");
hrDepartment.addEmployee(dina);
departmentRepository.save(hrDepartment);
/*
select
department0_.id as id1_0_,
department0_.name as name2_0_
from
department department0_
where
department0_.name =?
*/
var departmentFound = departmentRepository.findOne(name(salesDepartment.getName())).orElseThrow(() -> new DepartmentNotFoundException(salesDepartment.getName()));
assertThat(departmentFound, equalTo(salesDepartment));
/*
select distinct
department0_.id as id1_0_,
department0_.name as name2_0_
from
department department0_
left outer join
employee employees1_
on department0_.id = employees1_.department_id
where
employees1_.first_name =?
and employees1_.second_name =?
*/
var departmentsFound = departmentRepository.findAll(joinEmployees(firstName(chandler.getFirstName()).and(secondName(chandler.getSecondName()))));
assertThat(departmentsFound, contains(department(salesDepartment)));
/*
select distinct
department0_.id as id1_0_,
department0_.name as name2_0_
from
department department0_
left outer join
employee employees1_
on department0_.id = employees1_.department_id
where
employees1_.second_name =?
or employees1_.second_name =?
*/
departmentsFound = departmentRepository.findAll(joinEmployees(secondName(chandler.getSecondName()).or(secondName(dina.getSecondName()))));
assertThat(departmentsFound, containsInAnyOrder(department(salesDepartment), department(hrDepartment)));
/*
select distinct
department0_.id as id1_0_,
department0_.name as name2_0_
from
department department0_
left outer join
employee employees1_
on department0_.id = employees1_.department_id
where
employees1_.second_name <>?
*/
departmentsFound = departmentRepository.findAll(joinEmployees(not(secondName(rachel.getSecondName()))));
assertThat(departmentsFound, containsInAnyOrder(department(salesDepartment), department(hrDepartment)));
/*
select distinct
department0_.id as id1_0_0_,
employees1_.id as id1_1_1_,
department0_.name as name2_0_0_,
employees1_.date_of_birth as date_of_2_1_1_,
employees1_.department_id as departme5_1_1_,
employees1_.first_name as first_na3_1_1_,
employees1_.second_name as second_n4_1_1_,
employees1_.department_id as departme5_1_0__,
employees1_.id as id1_1_0__
from
department department0_
left outer join
employee employees1_
on department0_.id = employees1_.department_id
where
employees1_.second_name =?
*/
departmentsFound = departmentRepository.findAll(fetchEmployees(secondName(joey.getSecondName())));
assertThat(departmentsFound, containsInAnyOrder(department(salesDepartment), department(hrDepartment)));
var salesDepartmentFound = departmentsFound.stream().filter(salesDepartment::equals).findFirst().orElseThrow(() -> new DepartmentNotFoundException(salesDepartment.getName()));
assertThat(salesDepartmentFound.getEmployees(), contains(employee(joey)));
var hrDepartmentFound = departmentsFound.stream().filter(hrDepartment::equals).findFirst().orElseThrow(() -> new DepartmentNotFoundException(hrDepartment.getName()));
assertThat(hrDepartmentFound.getEmployees(), contains(employee(dina)));
/*
select distinct
department0_.id as id1_0_0_,
employees1_.id as id1_1_1_,
department0_.name as name2_0_0_,
employees1_.date_of_birth as date_of_2_1_1_,
employees1_.department_id as departme5_1_1_,
employees1_.first_name as first_na3_1_1_,
employees1_.second_name as second_n4_1_1_,
employees1_.department_id as departme5_1_0__,
employees1_.id as id1_1_0__
from
department department0_
left outer join
employee employees1_
on department0_.id = employees1_.department_id
where
employees1_.date_of_birth >=?
and employees1_.date_of_birth <?
*/
departmentsFound = departmentRepository.findAll(fetchEmployees(dateOfBirth(greaterThanOrEqualTo(LocalDate.of(1967, 1, 1)).and(lessThan(LocalDate.of(1968, 1, 1))))));
assertThat(departmentsFound, contains(department(salesDepartment)));
salesDepartmentFound = departmentsFound.stream().filter(salesDepartment::equals).findFirst().orElseThrow(() -> new DepartmentNotFoundException(salesDepartment.getName()));
assertThat(salesDepartmentFound.getEmployees(), containsInAnyOrder(employee(phoebe), employee(ross)));
/*
select distinct
department0_.id as id1_0_,
department0_.name as name2_0_
from
department department0_
left outer join
employee employees1_
on department0_.id = employees1_.department_id
where
employees1_.first_name =?
or employees1_.id in
(
?
)
*/
departmentsFound = departmentRepository.findAll(joinEmployees(firstName(phoebe.getFirstName()).or(in(List.of(rachel)))));
assertThat(departmentsFound, containsInAnyOrder(department(salesDepartment), department(financeDepartment)));
/*
The first specification in chain must be parametrized with a common subtype of the subsequent specifications.
This is the above example but with the order of specifications reversed.
*/
departmentsFound = departmentRepository.findAll(joinEmployees(ExpressionSpecifications.<Employee, Path<Employee>> in(List.of(rachel)).or(firstName(phoebe.getFirstName()))));
assertThat(departmentsFound, containsInAnyOrder(department(salesDepartment), department(financeDepartment)));
}
}