You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 {
0 commit comments