Skip to content

Commit 51ff07c

Browse files
committed
DATAJPA-765 - Upgraded to Querydsl 4.
1 parent a3662df commit 51ff07c

13 files changed

+113
-107
lines changed

pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<hsqldb1>1.8.0.10</hsqldb1>
2727
<jpa>2.0.0</jpa>
2828
<openjpa>2.4.0</openjpa>
29-
<querydsl>3.6.3</querydsl>
3029
<springdata.commons>1.12.0.BUILD-SNAPSHOT</springdata.commons>
3130

3231
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
@@ -223,14 +222,14 @@
223222

224223
<!-- QueryDsl -->
225224
<dependency>
226-
<groupId>com.mysema.querydsl</groupId>
225+
<groupId>com.querydsl</groupId>
227226
<artifactId>querydsl-apt</artifactId>
228227
<version>${querydsl}</version>
229228
<scope>provided</scope>
230229
</dependency>
231230

232231
<dependency>
233-
<groupId>com.mysema.querydsl</groupId>
232+
<groupId>com.querydsl</groupId>
234233
<artifactId>querydsl-jpa</artifactId>
235234
<version>${querydsl}</version>
236235
<optional>true</optional>
@@ -424,7 +423,7 @@
424423
<configuration>
425424
<logOnlyOnError>true</logOnlyOnError>
426425
<processors>
427-
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
426+
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
428427
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
429428
</processors>
430429
</configuration>

src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
3333
import org.springframework.data.querydsl.SimpleEntityPathResolver;
3434

35-
import com.mysema.query.jpa.JPQLQuery;
36-
import com.mysema.query.jpa.impl.JPAQuery;
37-
import com.mysema.query.types.EntityPath;
38-
import com.mysema.query.types.OrderSpecifier;
39-
import com.mysema.query.types.Predicate;
40-
import com.mysema.query.types.path.PathBuilder;
35+
import com.querydsl.core.types.EntityPath;
36+
import com.querydsl.core.types.OrderSpecifier;
37+
import com.querydsl.core.types.Predicate;
38+
import com.querydsl.core.types.dsl.PathBuilder;
39+
import com.querydsl.jpa.JPQLQuery;
40+
import com.querydsl.jpa.impl.AbstractJPAQuery;
4141

4242
/**
4343
* QueryDsl specific extension of {@link SimpleJpaRepository} which adds implementation for
@@ -46,8 +46,8 @@
4646
* @author Oliver Gierke
4747
* @author Thomas Darimont
4848
*/
49-
public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements
50-
QueryDslPredicateExecutor<T> {
49+
public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
50+
implements QueryDslPredicateExecutor<T> {
5151

5252
private static final EntityPathResolver DEFAULT_ENTITY_PATH_RESOLVER = SimpleEntityPathResolver.INSTANCE;
5353

@@ -78,6 +78,7 @@ public QueryDslJpaRepository(JpaEntityInformation<T, ID> entityInformation, Enti
7878
EntityPathResolver resolver) {
7979

8080
super(entityInformation, entityManager);
81+
8182
this.path = resolver.createPath(entityInformation.getJavaType());
8283
this.builder = new PathBuilder<T>(path.getType(), path.getMetadata());
8384
this.querydsl = new Querydsl(entityManager, builder);
@@ -89,7 +90,7 @@ public QueryDslJpaRepository(JpaEntityInformation<T, ID> entityInformation, Enti
8990
*/
9091
@Override
9192
public T findOne(Predicate predicate) {
92-
return createQuery(predicate).uniqueResult(path);
93+
return createQuery(predicate).select(path).fetchOne();
9394
}
9495

9596
/*
@@ -98,7 +99,7 @@ public T findOne(Predicate predicate) {
9899
*/
99100
@Override
100101
public List<T> findAll(Predicate predicate) {
101-
return createQuery(predicate).list(path);
102+
return createQuery(predicate).select(path).fetch();
102103
}
103104

104105
/*
@@ -107,7 +108,7 @@ public List<T> findAll(Predicate predicate) {
107108
*/
108109
@Override
109110
public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
110-
return executeSorted(createQuery(predicate), orders);
111+
return executeSorted(createQuery(predicate).select(path), orders);
111112
}
112113

113114
/*
@@ -116,7 +117,7 @@ public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
116117
*/
117118
@Override
118119
public List<T> findAll(Predicate predicate, Sort sort) {
119-
return executeSorted(createQuery(predicate), sort);
120+
return executeSorted(createQuery(predicate).select(path), sort);
120121
}
121122

122123
/*
@@ -125,7 +126,7 @@ public List<T> findAll(Predicate predicate, Sort sort) {
125126
*/
126127
@Override
127128
public List<T> findAll(OrderSpecifier<?>... orders) {
128-
return executeSorted(createQuery(new Predicate[0]), orders);
129+
return executeSorted(createQuery(new Predicate[0]).select(path), orders);
129130
}
130131

131132
/*
@@ -135,11 +136,11 @@ public List<T> findAll(OrderSpecifier<?>... orders) {
135136
@Override
136137
public Page<T> findAll(Predicate predicate, Pageable pageable) {
137138

138-
JPQLQuery countQuery = createQuery(predicate);
139-
JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate));
139+
JPQLQuery<?> countQuery = createQuery(predicate);
140+
JPQLQuery<T> query = querydsl.applyPagination(pageable, createQuery(predicate).select(path));
140141

141-
Long total = countQuery.count();
142-
List<T> content = pageable == null || total > pageable.getOffset() ? query.list(path) : Collections.<T> emptyList();
142+
long total = countQuery.fetchCount();
143+
List<T> content = pageable == null || total > pageable.getOffset() ? query.fetch() : Collections.<T> emptyList();
143144

144145
return new PageImpl<T>(content, pageable, total);
145146
}
@@ -150,7 +151,7 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
150151
*/
151152
@Override
152153
public long count(Predicate predicate) {
153-
return createQuery(predicate).count();
154+
return createQuery(predicate).fetchCount();
154155
}
155156

156157
/*
@@ -159,7 +160,7 @@ public long count(Predicate predicate) {
159160
*/
160161
@Override
161162
public boolean exists(Predicate predicate) {
162-
return createQuery(predicate).exists();
163+
return createQuery(predicate).fetchCount() > 0;
163164
}
164165

165166
/**
@@ -168,9 +169,9 @@ public boolean exists(Predicate predicate) {
168169
* @param predicate
169170
* @return the Querydsl {@link JPQLQuery}.
170171
*/
171-
protected JPQLQuery createQuery(Predicate... predicate) {
172+
protected JPQLQuery<?> createQuery(Predicate... predicate) {
172173

173-
JPAQuery query = querydsl.createQuery(path).where(predicate);
174+
AbstractJPAQuery<?, ?> query = querydsl.createQuery(path).where(predicate);
174175
CrudMethodMetadata metadata = getRepositoryMethodMetadata();
175176

176177
if (metadata == null) {
@@ -194,7 +195,7 @@ protected JPQLQuery createQuery(Predicate... predicate) {
194195
* @param orders must not be {@literal null}.
195196
* @return
196197
*/
197-
private List<T> executeSorted(JPQLQuery query, OrderSpecifier<?>... orders) {
198+
private List<T> executeSorted(JPQLQuery<T> query, OrderSpecifier<?>... orders) {
198199
return executeSorted(query, new QSort(orders));
199200
}
200201

@@ -205,7 +206,7 @@ private List<T> executeSorted(JPQLQuery query, OrderSpecifier<?>... orders) {
205206
* @param sort must not be {@literal null}.
206207
* @return
207208
*/
208-
private List<T> executeSorted(JPQLQuery query, Sort sort) {
209-
return querydsl.applySorting(sort, query).list(path);
209+
private List<T> executeSorted(JPQLQuery<T> query, Sort sort) {
210+
return querydsl.applySorting(sort, query).fetch();
210211
}
211212
}

src/main/java/org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import org.springframework.stereotype.Repository;
2323
import org.springframework.util.Assert;
2424

25-
import com.mysema.query.dml.DeleteClause;
26-
import com.mysema.query.dml.UpdateClause;
27-
import com.mysema.query.jpa.JPQLQuery;
28-
import com.mysema.query.jpa.impl.JPADeleteClause;
29-
import com.mysema.query.jpa.impl.JPAUpdateClause;
30-
import com.mysema.query.types.EntityPath;
31-
import com.mysema.query.types.path.PathBuilder;
32-
import com.mysema.query.types.path.PathBuilderFactory;
25+
import com.querydsl.core.dml.DeleteClause;
26+
import com.querydsl.core.dml.UpdateClause;
27+
import com.querydsl.core.types.EntityPath;
28+
import com.querydsl.core.types.dsl.PathBuilder;
29+
import com.querydsl.core.types.dsl.PathBuilderFactory;
30+
import com.querydsl.jpa.JPQLQuery;
31+
import com.querydsl.jpa.impl.JPADeleteClause;
32+
import com.querydsl.jpa.impl.JPAUpdateClause;
3333

3434
/**
3535
* Base class for implementing repositories using QueryDsl library.
@@ -88,12 +88,23 @@ protected EntityManager getEntityManager() {
8888
/**
8989
* Returns a fresh {@link JPQLQuery}.
9090
*
91+
* @param path must not be {@literal null}.
9192
* @return the Querydsl {@link JPQLQuery}.
9293
*/
93-
protected JPQLQuery from(EntityPath<?>... paths) {
94+
protected JPQLQuery<Object> from(EntityPath<?>... paths) {
9495
return querydsl.createQuery(paths);
9596
}
9697

98+
/**
99+
* Returns a {@link JPQLQuery} for the given {@link EntityPath}.
100+
*
101+
* @param path must not be {@literal null}.
102+
* @return
103+
*/
104+
protected <T> JPQLQuery<T> from(EntityPath<T> path) {
105+
return querydsl.createQuery(path).select(path);
106+
}
107+
97108
/**
98109
* Returns a fresh {@link DeleteClause}.
99110
*

src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@
2727
import org.springframework.data.querydsl.QSort;
2828
import org.springframework.util.Assert;
2929

30-
import com.mysema.query.jpa.EclipseLinkTemplates;
31-
import com.mysema.query.jpa.HQLTemplates;
32-
import com.mysema.query.jpa.JPQLQuery;
33-
import com.mysema.query.jpa.OpenJPATemplates;
34-
import com.mysema.query.jpa.impl.AbstractJPAQuery;
35-
import com.mysema.query.jpa.impl.JPAQuery;
36-
import com.mysema.query.support.Expressions;
37-
import com.mysema.query.types.EntityPath;
38-
import com.mysema.query.types.Expression;
39-
import com.mysema.query.types.OrderSpecifier;
40-
import com.mysema.query.types.OrderSpecifier.NullHandling;
41-
import com.mysema.query.types.Path;
42-
import com.mysema.query.types.path.PathBuilder;
30+
import com.querydsl.core.types.EntityPath;
31+
import com.querydsl.core.types.Expression;
32+
import com.querydsl.core.types.OrderSpecifier;
33+
import com.querydsl.core.types.OrderSpecifier.NullHandling;
34+
import com.querydsl.core.types.Path;
35+
import com.querydsl.core.types.dsl.Expressions;
36+
import com.querydsl.core.types.dsl.PathBuilder;
37+
import com.querydsl.jpa.EclipseLinkTemplates;
38+
import com.querydsl.jpa.HQLTemplates;
39+
import com.querydsl.jpa.JPQLQuery;
40+
import com.querydsl.jpa.OpenJPATemplates;
41+
import com.querydsl.jpa.impl.AbstractJPAQuery;
42+
import com.querydsl.jpa.impl.JPAQuery;
4343

4444
/**
4545
* Helper instance to ease access to Querydsl JPA query API.
@@ -74,18 +74,18 @@ public Querydsl(EntityManager em, PathBuilder<?> builder) {
7474
*
7575
* @return
7676
*/
77-
public AbstractJPAQuery<JPAQuery> createQuery() {
77+
public <T> AbstractJPAQuery<T, JPAQuery<T>> createQuery() {
7878

7979
switch (provider) {
8080
case ECLIPSELINK:
81-
return new JPAQuery(em, EclipseLinkTemplates.DEFAULT);
81+
return new JPAQuery<T>(em, EclipseLinkTemplates.DEFAULT);
8282
case HIBERNATE:
83-
return new JPAQuery(em, HQLTemplates.DEFAULT);
83+
return new JPAQuery<T>(em, HQLTemplates.DEFAULT);
8484
case OPEN_JPA:
85-
return new JPAQuery(em, OpenJPATemplates.DEFAULT);
85+
return new JPAQuery<T>(em, OpenJPATemplates.DEFAULT);
8686
case GENERIC_JPA:
8787
default:
88-
return new JPAQuery(em);
88+
return new JPAQuery<T>(em);
8989
}
9090
}
9191

@@ -94,7 +94,7 @@ public AbstractJPAQuery<JPAQuery> createQuery() {
9494
*
9595
* @return
9696
*/
97-
public AbstractJPAQuery<JPAQuery> createQuery(EntityPath<?>... paths) {
97+
public AbstractJPAQuery<Object, JPAQuery<Object>> createQuery(EntityPath<?>... paths) {
9898
return createQuery().from(paths);
9999
}
100100

@@ -105,7 +105,7 @@ public AbstractJPAQuery<JPAQuery> createQuery(EntityPath<?>... paths) {
105105
* @param query must not be {@literal null}.
106106
* @return the Querydsl {@link JPQLQuery}.
107107
*/
108-
public JPQLQuery applyPagination(Pageable pageable, JPQLQuery query) {
108+
public <T> JPQLQuery<T> applyPagination(Pageable pageable, JPQLQuery<T> query) {
109109

110110
if (pageable == null) {
111111
return query;
@@ -124,7 +124,7 @@ public JPQLQuery applyPagination(Pageable pageable, JPQLQuery query) {
124124
* @param query must not be {@literal null}.
125125
* @return the Querydsl {@link JPQLQuery}
126126
*/
127-
public JPQLQuery applySorting(Sort sort, JPQLQuery query) {
127+
public <T> JPQLQuery<T> applySorting(Sort sort, JPQLQuery<T> query) {
128128

129129
if (sort == null) {
130130
return query;
@@ -144,8 +144,7 @@ public JPQLQuery applySorting(Sort sort, JPQLQuery query) {
144144
* @param qsort must not be {@literal null}.
145145
* @param query must not be {@literal null}.
146146
*/
147-
148-
private JPQLQuery addOrderByFrom(QSort qsort, JPQLQuery query) {
147+
private <T> JPQLQuery<T> addOrderByFrom(QSort qsort, JPQLQuery<T> query) {
149148

150149
List<OrderSpecifier<?>> orderSpecifiers = qsort.getOrderSpecifiers();
151150
return query.orderBy(orderSpecifiers.toArray(new OrderSpecifier[orderSpecifiers.size()]));
@@ -159,7 +158,7 @@ private JPQLQuery addOrderByFrom(QSort qsort, JPQLQuery query) {
159158
* @param query must not be {@literal null}.
160159
* @return
161160
*/
162-
private JPQLQuery addOrderByFrom(Sort sort, JPQLQuery query) {
161+
private <T> JPQLQuery<T> addOrderByFrom(Sort sort, JPQLQuery<T> query) {
163162

164163
Assert.notNull(sort, "Sort must not be null!");
165164
Assert.notNull(query, "Query must not be null!");
@@ -180,9 +179,9 @@ private JPQLQuery addOrderByFrom(Sort sort, JPQLQuery query) {
180179
@SuppressWarnings({ "rawtypes", "unchecked" })
181180
private OrderSpecifier<?> toOrderSpecifier(Order order) {
182181

183-
return new OrderSpecifier(order.isAscending() ? com.mysema.query.types.Order.ASC
184-
: com.mysema.query.types.Order.DESC, buildOrderPropertyPathFrom(order),
185-
toQueryDslNullHandling(order.getNullHandling()));
182+
return new OrderSpecifier(
183+
order.isAscending() ? com.querydsl.core.types.Order.ASC : com.querydsl.core.types.Order.DESC,
184+
buildOrderPropertyPathFrom(order), toQueryDslNullHandling(order.getNullHandling()));
186185
}
187186

188187
/**

src/test/java/org/springframework/data/jpa/repository/CrudMethodMetadataUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,7 +42,7 @@
4242
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
4343

4444
/**
45-
* Integratio test for lock support.
45+
* Integration test for lock support.
4646
*
4747
* @author Oliver Gierke
4848
* @author Thomas Darimont

src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithEmbeddedId.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,19 +23,18 @@
2323
import org.springframework.data.jpa.repository.JpaRepository;
2424
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
2525

26-
import com.mysema.query.types.OrderSpecifier;
27-
import com.mysema.query.types.Predicate;
26+
import com.querydsl.core.types.OrderSpecifier;
27+
import com.querydsl.core.types.Predicate;
2828

2929
/**
3030
* Demonstrates the support for composite primary keys with {@code @EmbeddedId}.
3131
*
3232
* @author Thomas Darimont
3333
*/
3434
@Lazy
35-
public interface EmployeeRepositoryWithEmbeddedId extends
36-
JpaRepository<EmbeddedIdExampleEmployee, EmbeddedIdExampleEmployeePK>,
35+
public interface EmployeeRepositoryWithEmbeddedId
36+
extends JpaRepository<EmbeddedIdExampleEmployee, EmbeddedIdExampleEmployeePK>,
3737
QueryDslPredicateExecutor<EmbeddedIdExampleEmployee> {
3838

3939
List<EmbeddedIdExampleEmployee> findAll(Predicate predicate, OrderSpecifier<?>... orders);
40-
4140
}

0 commit comments

Comments
 (0)