Skip to content

Commit ee46165

Browse files
baezzysFBibonne
authored andcommitted
Add support for query hints in JPA Item Readers
Enhanced `JpaCursorItemReader`, `JpaCursorItemReaderBuilder`, `JpaPagingItemReader`, and `JpaPagingItemReaderBuilder` with query hints configuration. The inclusion of query hints in both cursor and paging item readers improves query execution strategies, optimizing performance for complex data retrieval scenarios. Resolves spring-projects#4479 Signed-off-by: Fabrice Bibonne <[email protected]>
1 parent e31e0e1 commit ee46165

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaCursorItemReader.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2024 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.
@@ -43,6 +43,7 @@
4343
* The implementation is <b>not</b> thread-safe.
4444
*
4545
* @author Mahmoud Ben Hassine
46+
* @author Jinwoo Bae
4647
* @param <T> type of items to read
4748
* @since 4.3
4849
*/
@@ -58,6 +59,8 @@ public class JpaCursorItemReader<T> extends AbstractItemCountingItemStreamItemRe
5859

5960
private Map<String, Object> parameterValues;
6061

62+
private Map<String, Object> hintValues;
63+
6164
private Iterator<T> iterator;
6265

6366
/**
@@ -100,6 +103,17 @@ public void setParameterValues(Map<String, Object> parameterValues) {
100103
this.parameterValues = parameterValues;
101104
}
102105

106+
/**
107+
* Set the query hint values for the JPA query. Query hints can be used to give
108+
* instructions to the JPA provider.
109+
* @param hintValues a map where each key is the name of the hint, and the
110+
* corresponding value is the hint's value.
111+
* @since 5.2
112+
*/
113+
public void setHintValues(Map<String, Object> hintValues) {
114+
this.hintValues = hintValues;
115+
}
116+
103117
@Override
104118
public void afterPropertiesSet() throws Exception {
105119
Assert.state(this.entityManagerFactory != null, "EntityManagerFactory is required");
@@ -123,6 +137,10 @@ protected void doOpen() throws Exception {
123137
if (this.parameterValues != null) {
124138
this.parameterValues.forEach(query::setParameter);
125139
}
140+
if (this.hintValues != null) {
141+
this.hintValues.forEach(query::setHint);
142+
}
143+
126144
this.iterator = query.getResultStream().iterator();
127145
}
128146

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaPagingItemReader.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2024 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.
@@ -80,6 +80,7 @@
8080
* @author Dave Syer
8181
* @author Will Schipp
8282
* @author Mahmoud Ben Hassine
83+
* @author Jinwoo Bae
8384
* @since 2.0
8485
*/
8586
public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
@@ -96,6 +97,8 @@ public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
9697

9798
private Map<String, Object> parameterValues;
9899

100+
private Map<String, Object> hintValues;
101+
99102
private boolean transacted = true;// default value
100103

101104
public JpaPagingItemReader() {
@@ -128,6 +131,17 @@ public void setParameterValues(Map<String, Object> parameterValues) {
128131
this.parameterValues = parameterValues;
129132
}
130133

134+
/**
135+
* Set the query hint values for the JPA query. Query hints can be used to give
136+
* instructions to the JPA provider.
137+
* @param hintValues a map where each key is the name of the hint, and the
138+
* corresponding value is the hint's value.
139+
* @since 5.2
140+
*/
141+
public void setHintValues(Map<String, Object> hintValues) {
142+
this.hintValues = hintValues;
143+
}
144+
131145
/**
132146
* By default (true) the EntityTransaction will be started and committed around the
133147
* read. Can be overridden (false) in cases where the JPA implementation doesn't
@@ -202,6 +216,10 @@ protected void doReadPage() {
202216
}
203217
}
204218

219+
if (this.hintValues != null) {
220+
this.hintValues.forEach(query::setHint);
221+
}
222+
205223
if (results == null) {
206224
results = new CopyOnWriteArrayList<>();
207225
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaCursorItemReaderBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2024 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.
@@ -30,6 +30,7 @@
3030
* Builder for {@link JpaCursorItemReader}.
3131
*
3232
* @author Mahmoud Ben Hassine
33+
* @author Jinwoo Bae
3334
* @since 4.3
3435
*/
3536
public class JpaCursorItemReaderBuilder<T> {
@@ -42,6 +43,8 @@ public class JpaCursorItemReaderBuilder<T> {
4243

4344
private Map<String, Object> parameterValues;
4445

46+
private Map<String, Object> hintValues;
47+
4548
private boolean saveState = true;
4649

4750
private String name;
@@ -112,6 +115,19 @@ public JpaCursorItemReaderBuilder<T> parameterValues(Map<String, Object> paramet
112115
return this;
113116
}
114117

118+
/**
119+
* A map of hint values to be set on the query. The key of the map is the name of the
120+
* hint to be applied, with the value being the specific setting for that hint.
121+
* @param hintValues map of query hints
122+
* @return this instance for method chaining
123+
* @see JpaCursorItemReader#setHintValues(Map)
124+
* @since 5.2
125+
*/
126+
public JpaCursorItemReaderBuilder<T> hintValues(Map<String, Object> hintValues) {
127+
this.hintValues = hintValues;
128+
return this;
129+
}
130+
115131
/**
116132
* A query provider. This should be set only if {@link #queryString(String)} have not
117133
* been set.
@@ -169,10 +185,12 @@ public JpaCursorItemReader<T> build() {
169185
reader.setQueryProvider(this.queryProvider);
170186
reader.setQueryString(this.queryString);
171187
reader.setParameterValues(this.parameterValues);
188+
reader.setHintValues(this.hintValues);
172189
reader.setCurrentItemCount(this.currentItemCount);
173190
reader.setMaxItemCount(this.maxItemCount);
174191
reader.setSaveState(this.saveState);
175192
reader.setName(this.name);
193+
176194
return reader;
177195
}
178196

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilder.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2024 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.
@@ -27,6 +27,7 @@
2727
*
2828
* @author Michael Minella
2929
* @author Glenn Renfro
30+
* @author Jinwoo Bae
3031
* @since 4.0
3132
*/
3233

@@ -38,6 +39,8 @@ public class JpaPagingItemReaderBuilder<T> {
3839

3940
private Map<String, Object> parameterValues;
4041

42+
private Map<String, Object> hintValues;
43+
4144
private boolean transacted = true;
4245

4346
private String queryString;
@@ -129,6 +132,19 @@ public JpaPagingItemReaderBuilder<T> parameterValues(Map<String, Object> paramet
129132
return this;
130133
}
131134

135+
/**
136+
* A map of hint values to be set on the query. The key of the map is the name of the
137+
* hint to be applied, with the value being the specific setting for that hint.
138+
* @param hintValues map of query hints
139+
* @return this instance for method chaining
140+
* @see JpaPagingItemReader#setHintValues(Map)
141+
* @since 5.2
142+
*/
143+
public JpaPagingItemReaderBuilder<T> hintValues(Map<String, Object> hintValues) {
144+
this.hintValues = hintValues;
145+
return this;
146+
}
147+
132148
/**
133149
* A query provider. This should be set only if {@link #queryString(String)} have not
134150
* been set.
@@ -204,6 +220,7 @@ public JpaPagingItemReader<T> build() {
204220
reader.setQueryString(this.queryString);
205221
reader.setPageSize(this.pageSize);
206222
reader.setParameterValues(this.parameterValues);
223+
reader.setHintValues(this.hintValues);
207224
reader.setEntityManagerFactory(this.entityManagerFactory);
208225
reader.setQueryProvider(this.queryProvider);
209226
reader.setTransacted(this.transacted);

0 commit comments

Comments
 (0)