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!
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.
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.
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.
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.
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.
For reference, you could also use CONCAT:
like CONCAT('%', :someSymbol, '%')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With