java - How can I run a runtime generated query with Pageable in Spring Data? -


i have runtime generated jpql string, , pageable object. want run jpql query pageable. how can that?

e.g.: want solution runtime generated query string.

@query("select u user u") page<user> findusers(pageable pageable); 

attempt 1

to use query generated @ runtime, repository method have declared as

@query("?1") page<user> findusers(string query, pageable pageable) 

however, @query parsed @ time of initializing application context, approach fail because ?1 on own not valid jpql expression.

attempt 2

if use

@query("?1", nativequery = true) 

the context initialize , able pass generated query method, pageable parameter ignored because query passed method parameter used as-is.

so, in short, no, looking cannot done pageable.


attempt 3

if must generate query @ runtime, must perform pagination well. example query mysql underlying database:

@query("select u user u order last_name, first_name offset (?1 - 1) * ?2 limit ?2", nativequery = true) page<user> findusers(int page, int pagesize); 

do note offset , limit specific mysql , postgres. there may different syntax other database products.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -