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)")
Related
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);
I'd like to select distinct records based on a column where not null and return a page. The following is throwing java.sql.SQLSyntaxErrorException: ORA-01791: not a SELECTed expression.
(If I can get this to work I'd like to select distinct by a few additional columns.)
#Query(value="SELECT DISTINCT pm.batchId from PrintManifest pm",
// + "where pm.batchId is not null"
countQuery = "select count(DISTINCT pm.batchId) from PrintManifest pm",
nativeQuery = true
)
Page<PolicyPrintManifest> findDistinctBatchId(Pageable pageable);
I also tried with no luck:
Page<PolicyPrintManifest> findDistinctByBatchIdExists(Pageable pageable);
What's the right way to do this query?
Is PrintManifest name of the table or the entity. Remember that in native query, you have to use table name not entity name
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 have a native query I need to change to HQL.
The original query is:
SELECT COUNT(DISTINCT `table`.`id`) FROM `database`.`table` WHERE
(`table`.`date`" " BETWEEN '" + year + "-01-01 00:00:00' AND '" + year
+ "-12-31 23:59:59') AND `table`.`box` NOT LIKE '' GROUP BY MONTH(`table`.`date`)";
I tried something like:
StringBuilder hql = new StringBuilder();
hql.append(" select count(distinct table.id)");
hql.append(" from Table table");
hql.append(" where table.date between '?-01-01 00:00:00' and '?-12-31 23:59:59'");
hql.append(" and table.box not like ''");
hql.append("group by month (table.date)");
query.setParameter(1, Integer.toString(year));
query.setParameter(2, Integer.toString(year));
Where year is a int passed to the method as argument.
The generated query is:
Hibernate: select count(distinct table0_.id) as col_0_0_ from table table0_ where (table0_.date between '2013-01-01 00:00:00' and '2013-12-31 23:59:59') and (table0_.box not like '') group by month(table0_.date)
My problem is: using the native query, I get one value and using the hql I get another for month 2 (February). For month 1 (January) results are the same.
What am I missing here?
Thanks in advance,
gtludwig
They seem to be the same query without the schema qualification to me. Aren't you running them in different instance? Like, one in 'database' and the other pointing to anoher base with similar data loaded?