Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a JPA named parameter surrounded by wildcards?

How would I specify a JPA query like:

Query q =    em.createQuery(     "SELECT x FROM org.SomeTable x WHERE x.someString LIKE '%:someSymbol%'"   ); 

followed by:

q.setParameter("someSymbol", "someSubstring"); 

and not triggering a

org.hibernate.QueryParameterException: could not locate named parameter [id] 

Much appreciated!

like image 290
Reuben Peter-Paul Avatar asked May 12 '11 19:05

Reuben Peter-Paul


People also ask

What is named parameter in JPA?

Named parameters are quite similar to positional parameters; however, by using them, we make the parameters more explicit and the query becomes more readable: TypedQuery<Employee> query = em. createQuery( "SELECT e FROM Employee e WHERE e.

What is the use of @query in JPA?

Understanding the @Query Annotation The @Query annotation can only be used to annotate repository interface methods. The call of the annotated methods will trigger the execution of the statement found in it, and their usage is pretty straightforward. The @Query annotation supports both native SQL and JPQL.

What is Nativequery JPA?

Native query refers to actual sql queries (referring to actual database objects). These queries are the sql statements which can be directly executed in database using a database client.

What is named parameter in hibernate?

Named query parameters are tokens of the form :name in the query string. A value is bound to the integer parameter :foo by calling setParameter("foo", foo, Hibernate. INTEGER); for example. A name may appear multiple times in the query string.


2 Answers

How about

Query q =    em.createQuery(     "SELECT x FROM org.SomeTable x WHERE x.someString LIKE :someSymbol" ); q.setParameter("someSymbol", "%someSubstring%"); 

I'm pretty sure I once solved your problem like that.

like image 182
musiKk Avatar answered Oct 13 '22 18:10

musiKk


For reference, you could also use CONCAT:

like CONCAT('%', :someSymbol, '%') 
like image 26
xtian Avatar answered Oct 13 '22 18:10

xtian