I have to write hql for one of the subquery. I am getting querysyntax exception.
Below is my code.
public List<URPTempSensor> findTempSensorObjs(String systemId, Character isLatest) {
Map<String,Object> params = new HashMap<String,Object>();
List<URPTempSensor> tSensorList = new ArrayList<URPTempSensor>();
params.put("systemId", systemId);
params.put("status", isLatest);
String sql =
"select * from " +
"(select tsensor.time, tsensor.tId from URPTempSensor tsensor where " +
"tsensor.isLatest=\'"+isLatest+"\' and tsensor.urpTempSystemId.systemId=\'"+systemId+"\' order by time desc)where rownum<=3";
tSensorList = this.urpTempDao.find(sql,params);
return tSensorList;
}
can anyone help on this
Probably you are receiving a syntax error because the HQL not support a SELECT after a the FROM clause:
"select * from " +
"(select
You need to rethink your SQL to write it on HQL.
Related
For example let my query be as shown below:
String query="select * from table_name where column_name1 > ?1 and column_name2 < ?2";
#Query(value = query, nativeQuery = true)
public List<Object[]> getResult(String filterValue1,Integer filterValue2);
how to pass the operator(<,>,=) also as a parameter?
I am using postgreSQL database.
Thank you.
If you have an option to construct/concat a String prior to run the query, there is no problem:
public String methodOne(String firstOperator
, String secondOperator) {
return "select * from table_name where column_name1 "
+ firstOperator + " ?1 and column_name2 "
+ secondOperator +" ?2";
}
It is more complicated if you use SpringData repositories.
There isn't a lot you can do with native queries parameters because SpringData is looking for native SQL operators inside the query string. But you can try to do some tricks with LIKE operator and built-in functions (in SQL, sometimes >,< can be replaced with LIKE)
(not completely an answer to your question, but)
A condition that can be omitted
#Query(value =
... AND column_name LIKE IIF(:myParam <> '%', :myParam,'%')
<skipped>
... repositoryMethod(#Param("myParam") String myParam);
IIF - a ternary operator function in MSSQL, you can find something like this in your RDBMS
when you send myParam='actualValue' it will be transformed into
and column_name LIKE 'actualValue'
i.e. column_name='actualValue'
when you send myParam='%' it will be transformed into
and column_name LIKE '%'
i.e. "and TRUE"
I'm using the below code to load tables from Access queries. It gives the sqlldr error so I printed the query string like shown below and found the order of the columns in the SELECT statement is not the same as in the query in the Access database. I'm very baffled by this. Has someone experienced the same thing?
sourceConn =
(UcanaccessConnection)DriverManager.getConnection("jdbc:ucanaccess://"
+ path);
List<Query> queries = sourceConn.getDbIO().getQueries();
for(Query query : queries) {
queryStr = query.toSQLString();
System.out.println("Query to process: " + queryStr);
}
I'm trying to add the Oracle Query Hint /*+ GATHER_PLAN_STATISTICS PARALLEL(16) */ to my Custom Query which looks similar to this:
#Query("SELECT " +
" FROM myTable tab " +
" WHERE tab.create_date >= to_date(:dateFrom) " +
" AND tab.create_date < to_date(:dateTo)")
List<MyModel> findEntriesByDate( #Param("dateFrom") String dateFrom, #Param("dateTo") String dateTo);
I cannot find any way to make this work. I tried various things, for example adding this on top of the method:
#QueryHints({
#QueryHint(name = org.hibernate.annotations.QueryHints.COMMENT, value = "GATHER_PLAN_STATISTICS")
})
or
#QueryHints({
#QueryHint(name = GATHER_PLAN_STATISTIC, value = "GATHER_PLAN_STATISTICS")
})
Is there a way to make this work or do I have to change my approach?
I am a novice to use jdbc and I have some problems.
I use hql to search data in MySQL, and the result is Query type. I don't know how to get the data from the "Query".This is my code:
final String hql = "select app.appkey,app.type from " + getClassName() +
"app where app.appkey<>'no-appkey' group by app.type";
Query query = getEntityManager().createQuery(hql);
Thanks a lot.
You have to do the following:
final String hql = "select app.appkey,app.type from " + getClassName() + " app where app.appkey<>'no-appkey' group by app.type";
Query query = getEntityManager().createQuery(hql);
query.list(); //or query.getSingleResult();
query.list() will give a list of results.
query.getSingleResult() will give you a object.
You can check this.
If you are expecting a list of results, so:
List<Object[]> results = query.getResultList();
If you are expect one single result:
Object[] result = query.getSingleResult(); // if more than one result was found, this method will throw a NonUniqueResultException
If column information will be stored in a position of the Object array. Example:
String appKey = (String) result[0];
String appType = (String) result[1];
But work with Object array is not good. Try to use Dto, like explained here.
Query query = em.createNativeQuery("SELECT * FROM IS_CON \n"
+ "WHERE CONNUMBER=?1 AND REV=?2", IsCon.class).setParameter(1, con).setParameter(2, rev);
is query return null.
But this query works.
Query query = em.createNativeQuery("SELECT * FROM IS_CON \n"
+ "WHERE CONNUMBER='"+con+"' AND REV='"+rev+"'", IsCon.class);
What is wrong my code? I think my code has nothing wrong.