I am using the @Query annotation in Spring JPA to write a custom query. I am having success when doing this for just 1 or 2 small strings -- however, I am trying to create 1 Param called "args" which could have several constraints/arguments.
For example, the "args" parameter is being passed as "title like '%iphone%'" (title%20like%20%27%25iphone%25%27);
The "addArgs" is additonal constraints like " and title like '%12%'" (and%20title%20like%20%27%2512%25%27 )
@Query(
value = "select :id as Id, :session as sessionId, :secondary as secondary, :primary as returnedData from :table where :args :addargs order by :order ;",
nativeQuery = true
)
List<TypeAhead> showMe(
@Param("id") String id,
@Param("session") String session,
@Param("primary") String primary,
@Param("secondary") String secondary,
@Param("table") String table,
@Param("args") String args,
@Param("addArgs") String addArgs,
@Param("order") String order
);
When I do this, I am getting the error message about SQL Syntax. I tried to log the SQL to the console; the System.out.println that I have written myself works fine when I paste into MySQL workbench, but doesn't work when using the :args parameter.
Below is how these Params are being logged by JPA:
2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [product_id]
2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [666]
2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [price]
2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [VARCHAR] - [title]
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [VARCHAR] - [products]
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [6] as [VARCHAR] - [title like '%iphone%']
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [7] as [VARCHAR] - [and title like '%12%']
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [8] as [VARCHAR] - [title asc]
Below is the query printed to the console using System.out.println - when I copy & paste this into MYSQL it's working fine.
select product_id as Id, 666 as session_id, price as secondary, title as returned_data from products where title like '%iphone%' and title like '%12%' order by title asc;
When I paste this into MySQL to test the query it comes back as expected & looks good. However I am getting errors when running the Java app.
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''products' where title like 'title like \'%iphone%\'' 'and title like \'%12%\'' ' at line 1
I am new to this so this may be a clunky way of doing this. Can someone help me understand what's going wrong, or why this isn't working as expected? Thanks in advance!
EDIT/UPDATE:
What is needed is to use EntityManager to dynamically build the query. https://www.baeldung.com/hibernate-entitymanager