Skip to content

Commit 06f5be3

Browse files
authored
Add subselect to JPQLRepository (#170)
Co-authored-by: Marvin Froeder <[email protected]>
1 parent f9c9a6d commit 06f5be3

File tree

13 files changed

+390
-28
lines changed

13 files changed

+390
-28
lines changed
Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import com.querydsl.core.dml.UpdateClause;
2222
import com.querydsl.core.types.EntityPath;
2323
import com.querydsl.core.types.Expression;
24+
import com.querydsl.core.types.dsl.Expressions;
25+
import com.querydsl.jpa.JPAExpressions;
2426
import com.querydsl.jpa.JPQLQuery;
2527
import com.querydsl.jpa.JPQLQueryFactory;
28+
import com.querydsl.jpa.JPQLSubQuery;
2629
import org.springframework.data.repository.NoRepositoryBean;
2730
import org.springframework.data.repository.Repository;
2831

2932
/** A repository which can access {@link JPQLQueryFactory} methods */
3033
@NoRepositoryBean
31-
public interface JpqlRepository<T, ID> extends Repository<T, ID> {
34+
public interface JPQLRepository<T, ID> extends Repository<T, ID> {
3235

3336
/**
3437
* Create a new DELETE clause
@@ -45,7 +48,7 @@ public interface JpqlRepository<T, ID> extends Repository<T, ID> {
4548
* @param <T>
4649
* @return select(expr)
4750
*/
48-
JPQLQuery<T> select(Expression<T> expr);
51+
<U> JPQLQuery<U> select(Expression<U> expr);
4952

5053
/**
5154
* Create a new JPQLQuery instance with the given projection
@@ -62,7 +65,7 @@ public interface JpqlRepository<T, ID> extends Repository<T, ID> {
6265
* @param <T>
6366
* @return select(distinct expr)
6467
*/
65-
JPQLQuery<T> selectDistinct(Expression<T> expr);
68+
<U> JPQLQuery<U> selectDistinct(Expression<U> expr);
6669

6770
/**
6871
* Create a new JPQLQuery instance with the given projection
@@ -93,7 +96,7 @@ public interface JpqlRepository<T, ID> extends Repository<T, ID> {
9396
* @param <T>
9497
* @return select(from).from(from)
9598
*/
96-
JPQLQuery<T> selectFrom(EntityPath<T> from);
99+
<U> JPQLQuery<U> selectFrom(EntityPath<U> from);
97100

98101
/**
99102
* Create a new UPDATE clause
@@ -110,4 +113,75 @@ public interface JpqlRepository<T, ID> extends Repository<T, ID> {
110113
* @return insert clause
111114
*/
112115
InsertClause<?> insert(EntityPath<T> path);
116+
117+
/**
118+
* Create a new detached JPQLQuery instance with the given projection
119+
*
120+
* @param expr projection
121+
* @param <T>
122+
* @return select(expr)
123+
*/
124+
default <U> JPQLSubQuery<U> subSelect(Expression<U> expr) {
125+
return JPAExpressions.select(expr);
126+
}
127+
128+
/**
129+
* Create a new detached JPQLQuery instance with the given projection
130+
*
131+
* @param exprs projection
132+
* @return select(exprs)
133+
*/
134+
default JPQLSubQuery<Tuple> subSelect(Expression<?>... exprs) {
135+
return JPAExpressions.select(exprs);
136+
}
137+
138+
/**
139+
* Create a new detached JPQLQuery instance with the given projection
140+
*
141+
* @param expr projection
142+
* @param <U>
143+
* @return select(distinct expr)
144+
*/
145+
default <U> JPQLSubQuery<U> subSelectDistinct(Expression<U> expr) {
146+
return JPAExpressions.select(expr).distinct();
147+
}
148+
149+
/**
150+
* Create a new detached JPQLQuery instance with the given projection
151+
*
152+
* @param exprs projection
153+
* @return select(distinct expr)
154+
*/
155+
default JPQLSubQuery<Tuple> subSelectDistinct(Expression<?>... exprs) {
156+
return JPAExpressions.select(exprs).distinct();
157+
}
158+
159+
/**
160+
* Create a new detached JPQLQuery instance with the projection zero
161+
*
162+
* @return select(0)
163+
*/
164+
default JPQLSubQuery<Integer> subSelectZero() {
165+
return subSelect(Expressions.ZERO);
166+
}
167+
168+
/**
169+
* Create a new detached JPQLQuery instance with the projection one
170+
*
171+
* @return select(1)
172+
*/
173+
default JPQLSubQuery<Integer> subSelectOne() {
174+
return subSelect(Expressions.ONE);
175+
}
176+
177+
/**
178+
* Create a new detached JPQLQuery instance with the given projection
179+
*
180+
* @param expr projection and source
181+
* @param <U>
182+
* @return select(expr).from(expr)
183+
*/
184+
default <U> JPQLSubQuery<U> subSelectFrom(EntityPath<U> expr) {
185+
return subSelect(expr).from(expr);
186+
}
113187
}

querydsl-jpa-spring/src/main/java/io/github/openfeign/querydsl/jpa/spring/repository/QuerydslJpaRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
* @author Michael Igler
2828
*/
2929
@NoRepositoryBean
30-
public interface QuerydslJpaRepository<T, ID> extends JpqlRepository<T, ID>, JpaRepository<T, ID> {}
30+
public interface QuerydslJpaRepository<T, ID> extends JPQLRepository<T, ID>, JpaRepository<T, ID> {}

querydsl-jpa-spring/src/main/java/io/github/openfeign/querydsl/jpa/spring/repository/support/QuerydslJpaRepositoryFactoryBean.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.github.openfeign.querydsl.jpa.spring.repository.support;
1717

18-
import io.github.openfeign.querydsl.jpa.spring.repository.JpqlRepository;
18+
import io.github.openfeign.querydsl.jpa.spring.repository.JPQLRepository;
1919
import jakarta.persistence.EntityManager;
2020
import org.springframework.beans.factory.FactoryBean;
2121
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
@@ -25,8 +25,8 @@
2525
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
2626
import org.springframework.data.repository.history.RevisionRepository;
2727

28-
/** {@link FactoryBean} creating {@link JpqlRepository} instances. */
29-
public class QuerydslJpaRepositoryFactoryBean<T extends JpqlRepository<S, ID>, S, ID>
28+
/** {@link FactoryBean} creating {@link JPQLRepository} instances. */
29+
public class QuerydslJpaRepositoryFactoryBean<T extends JPQLRepository<S, ID>, S, ID>
3030
extends JpaRepositoryFactoryBean<T, S, ID> {
3131

3232
/**

querydsl-jpa-spring/src/main/java/io/github/openfeign/querydsl/jpa/spring/repository/support/QuerydslJpaRepositoryImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.querydsl.core.types.Expression;
2424
import com.querydsl.jpa.JPQLQuery;
2525
import com.querydsl.jpa.impl.JPAQueryFactory;
26-
import io.github.openfeign.querydsl.jpa.spring.repository.JpqlRepository;
26+
import io.github.openfeign.querydsl.jpa.spring.repository.JPQLRepository;
2727
import jakarta.persistence.EntityManager;
2828
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
2929
import org.springframework.data.repository.core.EntityInformation;
@@ -32,7 +32,7 @@
3232

3333
/** Repository implementation using query dsl compiled queries */
3434
@Transactional(readOnly = true)
35-
public class QuerydslJpaRepositoryImpl<T, ID> implements JpqlRepository<T, ID> {
35+
public class QuerydslJpaRepositoryImpl<T, ID> implements JPQLRepository<T, ID> {
3636

3737
private final EntityInformation<T, ?> entityInformation;
3838
private final EntityManager entityManager;
@@ -62,7 +62,7 @@ public DeleteClause<?> delete(EntityPath<T> path) {
6262
}
6363

6464
@Override
65-
public JPQLQuery<T> select(Expression<T> expr) {
65+
public <U> JPQLQuery<U> select(Expression<U> expr) {
6666
return jpaQueryFactory.select(expr);
6767
}
6868

@@ -72,7 +72,7 @@ public JPQLQuery<Tuple> select(Expression<?>... exprs) {
7272
}
7373

7474
@Override
75-
public JPQLQuery<T> selectDistinct(Expression<T> expr) {
75+
public <U> JPQLQuery<U> selectDistinct(Expression<U> expr) {
7676
return jpaQueryFactory.selectDistinct(expr);
7777
}
7878

@@ -92,7 +92,7 @@ public JPQLQuery<Integer> selectZero() {
9292
}
9393

9494
@Override
95-
public JPQLQuery<T> selectFrom(EntityPath<T> from) {
95+
public <U> JPQLQuery<U> selectFrom(EntityPath<U> from) {
9696
return jpaQueryFactory.selectFrom(from);
9797
}
9898

querydsl-jpa/src/main/java/com/querydsl/jpa/JPAExpressions.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class JPAExpressions {
4444
* @param <T>
4545
* @return select(expr)
4646
*/
47-
public static <T> JPQLQuery<T> select(Expression<T> expr) {
47+
public static <T> JPQLSubQuery<T> select(Expression<T> expr) {
4848
return new JPASubQuery<Void>().select(expr);
4949
}
5050

@@ -54,7 +54,7 @@ public static <T> JPQLQuery<T> select(Expression<T> expr) {
5454
* @param exprs projection
5555
* @return select(exprs)
5656
*/
57-
public static JPQLQuery<Tuple> select(Expression<?>... exprs) {
57+
public static JPQLSubQuery<Tuple> select(Expression<?>... exprs) {
5858
return new JPASubQuery<Void>().select(exprs);
5959
}
6060

@@ -65,7 +65,7 @@ public static JPQLQuery<Tuple> select(Expression<?>... exprs) {
6565
* @param <T>
6666
* @return select(distinct expr)
6767
*/
68-
public static <T> JPQLQuery<T> selectDistinct(Expression<T> expr) {
68+
public static <T> JPQLSubQuery<T> selectDistinct(Expression<T> expr) {
6969
return new JPASubQuery<Void>().select(expr).distinct();
7070
}
7171

@@ -75,7 +75,7 @@ public static <T> JPQLQuery<T> selectDistinct(Expression<T> expr) {
7575
* @param exprs projection
7676
* @return select(distinct expr)
7777
*/
78-
public static JPQLQuery<Tuple> selectDistinct(Expression<?>... exprs) {
78+
public static JPQLSubQuery<Tuple> selectDistinct(Expression<?>... exprs) {
7979
return new JPASubQuery<Void>().select(exprs).distinct();
8080
}
8181

@@ -84,7 +84,7 @@ public static JPQLQuery<Tuple> selectDistinct(Expression<?>... exprs) {
8484
*
8585
* @return select(0)
8686
*/
87-
public static JPQLQuery<Integer> selectZero() {
87+
public static JPQLSubQuery<Integer> selectZero() {
8888
return select(Expressions.ZERO);
8989
}
9090

@@ -93,7 +93,7 @@ public static JPQLQuery<Integer> selectZero() {
9393
*
9494
* @return select(1)
9595
*/
96-
public static JPQLQuery<Integer> selectOne() {
96+
public static JPQLSubQuery<Integer> selectOne() {
9797
return select(Expressions.ONE);
9898
}
9999

@@ -104,7 +104,7 @@ public static JPQLQuery<Integer> selectOne() {
104104
* @param <T>
105105
* @return select(expr).from(expr)
106106
*/
107-
public static <T> JPQLQuery<T> selectFrom(EntityPath<T> expr) {
107+
public static <T> JPQLSubQuery<T> selectFrom(EntityPath<T> expr) {
108108
return select(expr).from(expr);
109109
}
110110

querydsl-jpa/src/main/java/com/querydsl/jpa/JPASubQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.querydsl.core.Tuple;
2222
import com.querydsl.core.types.Expression;
2323

24-
class JPASubQuery<T> extends JPAQueryBase<T, JPASubQuery<T>> {
24+
class JPASubQuery<T> extends JPAQueryBase<T, JPASubQuery<T>> implements JPQLSubQuery<T> {
2525

2626
JPASubQuery() {
2727
super(new DefaultQueryMetadata(), JPQLTemplates.DEFAULT);

0 commit comments

Comments
 (0)