Skip to content

Commit 495cfbc

Browse files
fix: properly compute search panes with related entities
Related: #159
1 parent be286ee commit 495cfbc

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/main/java/org/springframework/data/jpa/datatables/repository/DataTablesRepositoryImpl.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import jakarta.persistence.EntityManager;
44
import jakarta.persistence.criteria.CriteriaBuilder;
55
import jakarta.persistence.criteria.CriteriaQuery;
6+
import jakarta.persistence.criteria.Path;
67
import jakarta.persistence.criteria.Root;
78
import lombok.extern.slf4j.Slf4j;
8-
import org.hibernate.query.sqm.tree.domain.AbstractSqmFrom;
9-
import org.hibernate.query.sqm.tree.domain.SqmPath;
10-
import org.hibernate.query.sqm.tree.from.SqmAttributeJoin;
119
import org.springframework.data.domain.Page;
1210
import org.springframework.data.jpa.datatables.SpecificationBuilder;
1311
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
@@ -109,8 +107,10 @@ private SearchPanes computeSearchPanes(DataTablesInput input, Specification<T> s
109107
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
110108
CriteriaQuery<Object[]> query = criteriaBuilder.createQuery(Object[].class);
111109
Root<T> root = query.from(getDomainClass());
112-
query.multiselect(root.get(attribute), criteriaBuilder.count(root));
113-
query.groupBy(root.get(attribute));
110+
Path<?> path = getPath(root, attribute);
111+
112+
query.multiselect(path, criteriaBuilder.count(root));
113+
query.groupBy(path);
114114
query.where(specification.toPredicate(root, query, criteriaBuilder));
115115

116116
List<SearchPanes.Item> items = new ArrayList<>();
@@ -127,4 +127,13 @@ private SearchPanes computeSearchPanes(DataTablesInput input, Specification<T> s
127127
return new SearchPanes(options);
128128
}
129129

130+
private Path<?> getPath(Root<T> root, String attribute) {
131+
String[] parts = attribute.split("\\.");
132+
Path<?> path = root;
133+
for (String part : parts) {
134+
path = path.get(part);
135+
}
136+
return path;
137+
}
138+
130139
}

src/test/java/org/springframework/data/jpa/datatables/repository/EmployeeRepositoryTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,16 @@ void withSearchPanes() {
318318
Map<String, Set<String>> searchPanes = new HashMap<>();
319319
searchPanes.put("position", new HashSet<>(asList("Software Engineer", "Integration Specialist")));
320320
searchPanes.put("age", emptySet());
321+
searchPanes.put("office.city", emptySet());
321322

322323
input.setSearchPanes(searchPanes);
323324

324325
DataTablesOutput<Employee> output = getOutput(input);
325326
assertThat(output.getRecordsFiltered()).isEqualTo(3);
326327
assertThat(output.getSearchPanes()).isNotNull();
327328

329+
assertThat(output.getSearchPanes().getOptions().size()).isEqualTo(3);
330+
328331
assertThat(output.getSearchPanes().getOptions().get("position")).containsOnly(
329332
new SearchPanes.Item("Software Engineer", "Software Engineer", 2, 2),
330333
new SearchPanes.Item("Integration Specialist", "Integration Specialist", 1, 1)
@@ -334,6 +337,11 @@ void withSearchPanes() {
334337
new SearchPanes.Item("41", "41", 1, 1),
335338
new SearchPanes.Item("61", "61", 1, 1)
336339
);
340+
assertThat(output.getSearchPanes().getOptions().get("office.city")).containsOnly(
341+
new SearchPanes.Item("London", "London", 1, 1),
342+
new SearchPanes.Item("New York", "New York", 1, 1),
343+
new SearchPanes.Item("San Francisco", "San Francisco", 1, 1)
344+
);
337345
}
338346

339347
@Test

0 commit comments

Comments
 (0)