Code for executing Native SQL Query.
Session session = sessionFactory.getCurrentSession();
Query query = session.createSQLQuery(
"SELECT sum(hsd_plt_veh) as hsd_plt_veh,sum(randmCost) as randmCost"+
"from expenses_profit where monthk=:mon and yeark=:yr");
.setParameter("mon", 1)
.setParameter("yr", 2014);
Object obj=return query.uniqueResult();
When we use Native SQL it returns an object.
************************************************************************************************
If we need column names along with the Object use below code.
Session session = sessionFactory.getCurrentSession();
Query query = session.createSQLQuery(
"SELECT sum(hsd_plt_veh) as hsd_plt_veh,sum(randmCost) as randmCost"+
"from expenses_profit where monthk=:mon and yeark=:yr");
.setParameter("mon", 1)
.setParameter("yr", 2014);
Map<String, String> expensesProfits =(Map<String, String>) query.uniqueResult();
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
//contains column names and values
List<Map<String,Object>> aliasToValueMapList=query.list();
**********************************************************************************
If we use
.addEntity(Cost.class) ;
All checks for all columns in the table in query.
Suppose id is 1st column if we dont mention in query
It gives following error.
ERROR org.hibernate.util.JDBCExceptionReporter - Column 'id' not found.