Named Parameter not getting recognized in jdbcTemplate.queryForList() method - java-8

I am getting below exception :
ERROR 1 --- [eduler_Worker-3] jdbc.sqltiming
: 123. PreparedStatement.setObject(1, {ids=[33925]})
com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of
range.
Caused by:
org.springframework.dao.TransientDataAccessResourceException:
PreparedStatementCallback; SQL [select * from tableA t1 WHERE t1.id IN
(:ids)]; The index 1 is out of range.; nested exception is
com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of
range.
Below is the code:
#Override
#SneakyThrows
public void publish(List<Long> ids) {
val result = jdbcTemplate.queryForList("select * from tableA t1 WHERE t1.id IN (:ids)",ImmutableMap.of("ids", ids));
}
I am unable to understand that why is it trying to put at index 1 while I am using named parameter in the query. Please help

Related

QuerySyntaxException occurred for JPA #Query for Distinct and Count

I am using the below as a JpaRepository interface method query
#Query("SELECT DISTINCT order.status, COUNT(*) FROM OrderEntity order WHERE order.customerNumber = ?1 GROUP BY order.status")
During app startup I am getting the following exception -
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: order near line 1, column 17 [SELECT DISTINCT order.status AS status, COUNT(*) AS count FROM debomitra.cmw.orders.entity.OrderEntity order WHERE order.customerNumber = ?1 GROUP BY order.status]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:297)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:600)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:709)
... 116 more
Is there any other way to write DISTINCT/COUNT query in JPA or am I doing something wrong?
Please provide me with some pointers.
I think the query will work if you modify it like below-
SELECT DISTINCT OrderEntity.status, COUNT(*) FROM OrderEntity WHERE OrderEntity.customerNumber = ?1 GROUP BY OrderEntity.status
The issue is with the order keyword in the JPQL. It is expecting a "by" keyword after the "order".
Whoever is reading this try avoiding "order" keyword in a statement expect at the end where you really need to issue a ORDER BY statement.

Oracle is having issues with hibernate generated query, generates query with _ in row num

I am using hibernate and using criteria query, database is Oracle. The below is the query that gets generated.
"
WITH query AS (
SELECT inner_query.*,
ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__
FROM (
select this_.MODIFIEDTIME as MODIFIED1_1_1_,
this_.FIELDNAME as FIELDNAM2_1_1_,
this_.EMPNBR as EMPNBR3_1_1_
from EMPLOYEEAUDIT this_
left outer join EMPLOYEE employee2_ on this_.EMPNBR=employee2_.EMPNBR
) inner_query
)
SELECT MODIFIED1_1_1_,FIELDNAM2_1_1_,EMPNBR3_1_1_
FROM query WHERE __hibernate_row_nr__ >= 1
AND __hibernate_row_nr__ < 10;"
It is throwing the below error:-
SQL Error: 911, SQLState: 22019
ORA-00911: invalid character
However, if i take the SQL query and repalce the hibernate_row_nr with hibernate_row_nr__ and run it in DbVisualizer it works. It seems Oracle is not able to recognize starting '_' as valid characters. How to fix this issue?
Below is my java code:-
public List<Employee> getEmployeeList(int start, int limit, AgSort sorter) throws UIFilterException {
DetachedCriteria detached = getEmployeeListCriteria(sorter, filters);
return (List<DealSetupAudit>) hibernateTemplate.findByCriteria(detached, start, limit);
}
private DetachedCriteria getEmployeeListCriteria(AgSort sorter) throws UIFilterException {
DetachedCriteria detached = getBaseCriteria(DealSetupAudit.class, sorter);
return detached;
}
protected <T> DetachedCriteria getBaseCriteria(Class<T> genericType, AgSort sorter) throws UIFilterException {
DetachedCriteria criteria = DetachedCriteria.forClass(genericType);
addSorter(criteria, sorter);
return criteria;
}
protected void addSorter(DetachedCriteria criteria, AgSort sort) {
if (sort != null) {
Order order = null;
if (sort.getDir().equalsIgnoreCase(Constants.DESC)) {
order = Order.desc(sort.getColumn());
} else {
order = Order.asc(sort.getColumn());
}
criteria.addOrder(order);
}
}
I am using Hibernate4, below is my application.properties :-
oracle.datasource.driver-class-name=oracle.jdbc.OracleDriver
oracle.datasource.url=******
oracle.datasource.username=*****
oracle.datasource.password=*****
oracle.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
oracle.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
Double quotes? Usually a really, really bad idea. I'd suggest you to avoid such things and use valid column names / aliases.
SQL> select dname _invalid_alias_
2 from dept
3 where deptno = 10;
select dname _invalid_alias_
*
ERROR at line 1:
ORA-00911: invalid character
SQL> select dname "_valid_alias_"
2 from dept
3 where deptno = 10;
_valid_alias_
--------------
ACCOUNTING
SQL>

Query syntax error on HQL Query

Hql query:
Select ecd.consumption from com.dventus.wonchi.jaxb.messages.Wonchi as w
join w.systemBound as consumption
join consumption.fixedNetwork as ecd
where w.meterId = 'DVEHighLowUsageCheck' and ecd.time in (select min(ecd.time))
Exception:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [Select ecd.consumption from com.dventus.wonchi.jaxb.messages.Wonchi as w join w.systemBound as consumption join consumption.fixedNetwork as ecd where w.meterId = 'DVEHighLowUsageCheck' and ecd.time in (select min(ecd.time))]
As pointed for #getjackx, the syntax is incorrect.
You need add the clause FROM for the subselect select min(ecd.time), like:
select min(ecd2.time) FROM FixedNetwork ec2

Select SUM() throws "Column not found" exception [42122-193] in H2

The execution of the following statement:
SELECT SUM(p.price)
FROM Product p
via JDBC on an embedded H2 database throws exception:
org.h2.jdbc.JdbcSQLException: Column "price" not found
[42122-193]
How can I make it work?
You have to use an alias for the column used in SUM():
SELECT SUM(p.price) as price
FROM Product p
Beware! The alias has to be named exactly as the column.

Hive String index out of range error

I am trying to execute this below query in Hive(cloudera),
select a.col1,a.col2
FROM t1 a LEFT SEMI JOIN (select * from t2 where y = 0) b on (a.col1 =b.x);
Below is the error I am getting,
Your query has the following error(s):
Error while compiling statement: FAILED: StringIndexOutOfBoundsException String index out of range: 0
I am getting this even if i run just select * from t2 where y = 0. But the same query is working fine in Impala. Any suggestion?
Thanks in advance.
The might have failed because there is an empty as in select

Resources