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

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -