I've read the documentation on inserting method parameters into queries and other questions however they all suggest that this should work.
#Query(value = "SELECT * FROM period WHERE time LIKE \":day%\" AND id IN (SELECT id FROM booking)", nativeQuery = true)
List<Booking> findAllBookingsOnDay(#Param("day") String day);
Results in:
Hibernate: SELECT * FROM period WHERE time LIKE ":day%" AND id IN (SELECT id FROM booking)
I've tried removing quotations and percentage but that just results in.
Hibernate: SELECT * FROM period WHERE time LIKE ? AND id IN (SELECT id FROM booking)
The correct way to do this is to just use an unescaped placeholder in the query, and the bind the LIKE wildcard expression to it from the Java side.
#Query(value = "SELECT * FROM period WHERE time LIKE :day AND id IN (SELECT id FROM booking)", nativeQuery =true)
List<Booking> findAllBookingsOnDay(#Param("day") String day);
Usage:
String day = "%friday%"; // or whatever value would make sense here
findAllBookingsOnDay(day);
Related
I want to do this operation in my java CassandraRepository with using #Query annotation:
select * from TABLE_A where month IN
('2021-01',
'2021-02',
'2021-03',
'2021-04',
'2021-05',
'2021-06',
'2021-07',
'2021-08',
'2021-09',
'2021-10',
'2021-11',
'2021-12')
AND user_id = '1';
In my CassandraRepository:
#Query("SELECT * FROM TABLE_A WHERE user_id = ?0 AND month IN ?1")
List<UserListenCountByMonth> findByUserIdAndYear(String userId, List<String> year);
But it gets error:
Caused by: com.datastax.driver.core.exceptions.SyntaxError: line 1:84
no viable alternative at input '2021-01' (...WHERE user_id = '1' AND [month] IN...)
I checked on debug and there is no problem with my String List. What am I missing? Thank you.
Old versions of CassandraRepository need parentheses in your #Query.
So, change:
#Query("SELECT * FROM TABLE_A WHERE user_id = ?0 AND month IN ?1")
to:
#Query("SELECT * FROM TABLE_A WHERE user_id = ?0 AND month IN (?1)")
I want to send parameter from property file in JPQL using #Query
Suppose I have a query,
#Query(value= "Select * from myTable where id = ?1 AND last_modified_date between DATE_SUB(NOW(), INTERVAL ?2) AND NOW()", nativeQuery = true)
List<MyTableClass> getAllData(Long id, String stringSetFromPropertyFile);
Here the "stringSetFromPropertyFile" is sent from the Service class.
When I do the above thing it gives a query error where ?2 as a parameter is not set blank.
try
#Query(value= "Select * from myTable where id = ?1 AND last_modified_date between DATE_SUB(NOW(), INTERVAL ?2) AND NOW()",nativeQuery = true)
List<MyTableClass> getAllData(Long id, String stringSetFromPropertyFile);
I have a procedure written in database. my_procedure(val1, val2)
So, lets say I have a database query like this:
select field1 as fieldName, field2 as fieldId
(select * from mydb.my_procedure(id)) as aValue // A procedure call
from mydb.my_table
I want to convert this to Spring's Data JPA #Query. Something like this:
#Query (" HERE I WANT THE ABOVE QUERY TO IMPLEMENT")
public List<MyTable> getDetails ()
Are we allowed to do this?
UPDATE
For example I have below query which I want to convert.
select id, name, roll,
(select * from db.calculate_fee (date, id)) fee
from Student
where id = 1 AND roll = 5
I want to do something like
#Query("SELECT student, (select * from db.calculate_fee (date, id) fee FROM Student student "
+ "WHERE student.id=:id, "
+ "AND student.name=:roll")
public List<Student> getDetails(#Param("id") Integer id, #Param("roll") Integer roll);
Is there anyway to do this?
Have you tried it with a native query?
#Query(nativeQuery = true, value = "select field1 as fieldName, field2 as fieldId...")
List<MyTable> getDetails();
(by the way, public is not needed for 'getDetails' as Repositories are interfaces)
#NamedStoredProcedureQueries({
#NamedStoredProcedureQuery(name = "getAllEmployees",
procedureName = "get_all_employees",
resultClasses = Employees.class)
})
#Procedure(name = "getAllEmployees")
List<Employees> getAllEmployees();
Complete tutorial
I'm trying to do something like this using native query:
#Query(value = "select * from systems where UPPER(system_name) like UPPER('%?1%') ORDER BY system_name LIMIT '?2'", nativeQuery = true)
List<System> findAllBySystemNameWithLimit(String systemName, int limit);
But it doesn't work. Is it possible to use limit as parameter? Thanks for answer.
When you use ?1 or :paramname you don't have to use them between two quotes like you do '%?1%' and '?2' so instead your query should look like :
select * from systems
where UPPER(system_name) like UPPER(CONCAT('%', ?1, '%')) ORDER BY system_name LIMIT ?2
//------------------------------^-----^-----^----^---^-------------------------------^^
I am trying to make a query that would build a DTO object :
#Query("Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, IF(a.salarie,'yes','no'))
from Activity a where a.year = ?1 and a.month = ?2")
List<AnalyseProductionDTO> getAnalyseProduction(Year year, Month month);
It doesn't work when I put a IF statement in the query.
I am getting a
java.lang.IllegalArgumentException: Validation failed for query
It works when I do :
#Query("Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, 'yes')
from Activity a where a.year = ?1 and a.month = ?2")
List<AnalyseProductionDTO> getAnalyseProduction(Year year, Month month);
Isn't it possible to include an IF statement in a constructor used in a query?
By default #Query doesn't allow native queries. The query should be either HQL if the ORM is hibernate or anything that is supported by the underlying ORM.
Since IF() function is native to MySQL and not supported by ORM query languages its validation will fail.
You can try the following with #Query.nativeQuery set to true.
#Query(value = "Select new com.mycorp.rh.web.rest.dto.AnalyseProductionDTO(a.client.name, IF(a.salarie,'yes','no'))
from Activity a where a.year = ?1 and a.month = ?2", nativeQuery = true)