Skip to content

Commit 1ca26d8

Browse files
committed
Polishing.
Fix post-rebase conflicts. See #3622
1 parent 2ce55db commit 1ca26d8

File tree

1 file changed

+0
-83
lines changed

1 file changed

+0
-83
lines changed

src/main/antora/modules/ROOT/pages/jpa/query-methods.adoc

-83
Original file line numberDiff line numberDiff line change
@@ -170,89 +170,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
170170
----
171171
====
172172

173-
[[jpa.query-methods.query-rewriter]]
174-
=== Applying a QueryRewriter
175-
176-
Sometimes, no matter how many features you try to apply, it seems impossible to get Spring Data JPA to apply every thing
177-
you'd like to a query before it is sent to the `EntityManager`.
178-
179-
You have the ability to get your hands on the query, right before it's sent to the `EntityManager` and "rewrite" it.
180-
That is, you can make any alterations at the last moment.
181-
Query rewriting applies to the actual query and, when applicable, to count queries.
182-
Count queries are optimized and therefore, either not necessary or a count is obtained through other means, such as derived from a Hibernate `SelectionQuery`.
183-
184-
185-
.Declare a QueryRewriter using `@Query`
186-
====
187-
[source, java]
188-
----
189-
public interface MyRepository extends JpaRepository<User, Long> {
190-
191-
@NativeQuery(value = "select original_user_alias.* from SD_USER original_user_alias",
192-
queryRewriter = MyQueryRewriter.class)
193-
List<User> findByNativeQuery(String param);
194-
195-
@Query(value = "select original_user_alias from User original_user_alias",
196-
queryRewriter = MyQueryRewriter.class)
197-
List<User> findByNonNativeQuery(String param);
198-
}
199-
----
200-
====
201-
202-
This example shows both a native (pure SQL) rewriter as well as a JPQL query, both leveraging the same `QueryRewriter`.
203-
In this scenario, Spring Data JPA will look for a bean registered in the application context of the corresponding type.
204-
205-
You can write a query rewriter like this:
206-
207-
.Example `QueryRewriter`
208-
====
209-
[source, java]
210-
----
211-
public class MyQueryRewriter implements QueryRewriter {
212-
213-
@Override
214-
public String rewrite(String query, Sort sort) {
215-
return query.replaceAll("original_user_alias", "rewritten_user_alias");
216-
}
217-
}
218-
----
219-
====
220-
221-
You have to ensure your `QueryRewriter` is registered in the application context, whether it's by applying one of Spring Framework's
222-
`@Component`-based annotations, or having it as part of a `@Bean` method inside an `@Configuration` class.
223-
224-
Another option is to have the repository itself implement the interface.
225-
226-
.Repository that provides the `QueryRewriter`
227-
====
228-
[source, java]
229-
----
230-
public interface MyRepository extends JpaRepository<User, Long>, QueryRewriter {
231-
232-
@Query(value = "select original_user_alias.* from SD_USER original_user_alias",
233-
nativeQuery = true,
234-
queryRewriter = MyRepository.class)
235-
List<User> findByNativeQuery(String param);
236-
237-
@Query(value = "select original_user_alias from User original_user_alias",
238-
queryRewriter = MyRepository.class)
239-
List<User> findByNonNativeQuery(String param);
240-
241-
@Override
242-
default String rewrite(String query, Sort sort) {
243-
return query.replaceAll("original_user_alias", "rewritten_user_alias");
244-
}
245-
}
246-
----
247-
====
248-
249-
Depending on what you're doing with your `QueryRewriter`, it may be advisable to have more than one, each registered with the
250-
application context.
251-
252-
NOTE: In a CDI-based environment, Spring Data JPA will search the `BeanManager` for instances of your implementation of
253-
`QueryRewriter`.
254-
255-
256173
[[jpa.query-methods.at-query.advanced-like]]
257174
=== Using Advanced `LIKE` Expressions
258175

0 commit comments

Comments
 (0)