Native query setParameter difference - nativequery

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.

Related

How to query the database based on current day using Hibernate?

I would like to retrieve database items based on the current day.
For that purpose I am trying to use the day() function as explained there and mentioned there.
I have a 'created' date field that is automatically filled when a new item is created in the table. It's configured using a model, like that:
#Column(name = "created", nullable = false)
#CreationTimestamp
private Date created;
I created a couple of items and the date is filled in the database as expected.
Now I am trying to call those items using a HQL query:
String hql = "" +
"FROM Item as item " +
"WHERE item.itemId = ?1" +
"AND item.created = day(current_date())";
The problem is that this query is returning a NoResultException.
It's weird because I am using the current_date() function in other scenarios, e.g.:
String hql = "" +
"FROM Item as item " +
"WHERE item.itemId = ?1" +
"AND item.createdAt >= current_date - ?2";
And it is working pretty well!
So I assume the issue is related with day() function.
Try use this:
String hql = "" +
"FROM Item as item " +
"WHERE item.itemId = ?1" +
"AND date(item.created) = current_date()";

UCanAccess getQueries alters the order of columns in SELECT statment

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);
}

How can i add a QueryHint to a Hibernate Custom Query?

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?

HQL subqueries in hibernate

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.

JDBC ResultSet get column from different tables

i wan to retrieve data from query involving many tables.
i have a query as follows
String sql = "SELECT "
+ "s.Food_ID AS 'Sales_FoodID', "
+ "f.Food_Name AS 'foodName' "
+ "FROM Ordering o, Sales s, Food f"
+ " WHERE o.Table_No = " + tableNo + ""
+ " AND o.Paid = '" + NOT_PAID + "'"
+ " AND s.Order_ID = o.Order_ID"
+ " AND f.Food_ID = s.Food_ID;";
resultSet = statement.executeQuery(sql);
no error were found when i run the program, but after i add this line to get a table's column data:
String orderID = resultSet.getString("foodName");
i'm given this error:
java.sql.SQLException: Column not found
anyone know why?
You have to use next() method.
You should know that ResultSet is implicitly positioned on position before first row so you need to call next to get current position and if is valid, it returns true, else returns false (cursor is positioned after the last row).
rs = statement.executeQuery(sql);
while (rs.next()) {
String orderID = rs.getString(2);
}
Note: You can use also rs.getString(<columnName>) but in case when you know how your statement looks i recommend to you use index instead of columnName.
After calling the resultSet.executeQuery() you need to call the next() to pulling the records from db
after that you can call setXxx() provided by Java API

Resources