java - Corresponding Spring JPA method name for requesting a random row in SQL -


in order record in sql table name, using following query:

select * user user.name = name; 

and corresponding spring jpa method name following:

userentity finduserbyname(@param("name") string name); 

my question following:

how can request random record sql table?
know sql query should following:

select * user order rand() limit 1; 

but, should corresponding spring jpa method name that?

userentity finduserxxxxxxx (xxxxxxx); 

jpa supports functions defined in specification. can use native query option or jpa 2.1 function call database functions not directly supported jpa specification. can use @query annotation in spring data jpa repository.

native query

@query(value="select * user order rand() limit 1", nativequery = true) userentity finduser(); 

function

@query("select u userentity u order function('rand')") list<userentity> finduser(); 

you can use list.get(0) single user.


Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -