|
15 | 15 | */ |
16 | 16 | package org.springframework.data.jpa.repository.query; |
17 | 17 |
|
18 | | -import jakarta.persistence.criteria.CriteriaBuilder; |
19 | | -import jakarta.persistence.criteria.CriteriaQuery; |
20 | | -import jakarta.persistence.criteria.Expression; |
21 | | -import jakarta.persistence.criteria.Predicate; |
22 | | -import jakarta.persistence.criteria.Root; |
| 18 | +import jakarta.persistence.EntityManager; |
23 | 19 |
|
24 | 20 | import org.springframework.data.domain.Sort; |
| 21 | +import org.springframework.data.jpa.repository.support.JpqlQueryTemplates; |
25 | 22 | import org.springframework.data.repository.query.ReturnedType; |
26 | 23 | import org.springframework.data.repository.query.parser.PartTree; |
27 | | -import org.springframework.lang.Nullable; |
28 | 24 |
|
29 | 25 | /** |
30 | 26 | * Special {@link JpaQueryCreator} that creates a count projecting query. |
|
36 | 32 | */ |
37 | 33 | public class JpaCountQueryCreator extends JpaQueryCreator { |
38 | 34 |
|
39 | | - private boolean distinct; |
| 35 | + private final boolean distinct; |
| 36 | + private final ReturnedType returnedType; |
40 | 37 |
|
41 | 38 | /** |
42 | | - * Creates a new {@link JpaCountQueryCreator}. |
| 39 | + * Creates a new {@link JpaCountQueryCreator} |
43 | 40 | * |
44 | 41 | * @param tree |
45 | | - * @param type |
46 | | - * @param builder |
| 42 | + * @param returnedType |
47 | 43 | * @param provider |
| 44 | + * @param templates |
| 45 | + * @param em |
48 | 46 | */ |
49 | | - public JpaCountQueryCreator(PartTree tree, ReturnedType type, CriteriaBuilder builder, |
50 | | - ParameterMetadataProvider provider) { |
| 47 | + public JpaCountQueryCreator(PartTree tree, ReturnedType returnedType, ParameterMetadataProvider provider, |
| 48 | + JpqlQueryTemplates templates, EntityManager em) { |
51 | 49 |
|
52 | | - super(tree, type, builder, provider); |
| 50 | + super(tree, returnedType, provider, templates, em); |
53 | 51 |
|
54 | 52 | this.distinct = tree.isDistinct(); |
| 53 | + this.returnedType = returnedType; |
55 | 54 | } |
56 | 55 |
|
57 | 56 | @Override |
58 | | - protected CriteriaQuery<? extends Object> createCriteriaQuery(CriteriaBuilder builder, ReturnedType type) { |
| 57 | + protected JpqlQueryBuilder.AbstractJpqlQuery buildQuery(Sort sort) { |
59 | 58 |
|
60 | | - return builder.createQuery(Long.class); |
61 | | - } |
62 | | - |
63 | | - @Override |
64 | | - @SuppressWarnings("unchecked") |
65 | | - protected CriteriaQuery<? extends Object> complete(@Nullable Predicate predicate, Sort sort, |
66 | | - CriteriaQuery<? extends Object> query, CriteriaBuilder builder, Root<?> root) { |
67 | | - |
68 | | - CriteriaQuery<? extends Object> select = query.select(getCountQuery(query, builder, root)); |
69 | | - return predicate == null ? select : select.where(predicate); |
70 | | - } |
| 59 | + JpqlQueryBuilder.SelectStep selectStep = JpqlQueryBuilder.selectFrom(returnedType.getDomainType()); |
| 60 | + if (this.distinct) { |
| 61 | + selectStep = selectStep.distinct(); |
| 62 | + } |
71 | 63 |
|
72 | | - @SuppressWarnings("rawtypes") |
73 | | - private Expression getCountQuery(CriteriaQuery<?> query, CriteriaBuilder builder, Root<?> root) { |
74 | | - return distinct ? builder.countDistinct(root) : builder.count(root); |
| 64 | + return selectStep.count(); |
75 | 65 | } |
76 | 66 | } |
0 commit comments