Hibernate version: Hibernate 3.0.5
Using criteria I have to retrieve some columns using aggregate funtions from one table itself.
The sql query looks like this
"select count(emp_id),sum(savings_sal),sum(misc_sal) from emp_table
where condition1 and conditon2 and conditon 3"
Here I receive the condition parameters from a jsp page and I may recieve any combination of conditions(i.e I may receive only 1 condition or all conditons,.........etc like that).
I am using J2EE struts frame work so I am writing this criteria in DAO .
As I need only 3 columns from the table I used projections and written the query
Code:
List empDetailsList = session.createCriteria(EmpBean.class)
.setProjection (Projections.projectionList()
.add(Projections.count("empID"))
.add(Projections.sum("savings_sal"))
.add(Projections.sum("misc_sal"))
)
.add(Restrictions.eq(condition1))
.add(Restrictions.in(condition2))
.add(Restrictions.gt( condition 3)).list()
MyQUESTION here is how to retrieve these field values{count("empID"),sum("savings_sal"), sum("misc_sal")} inDAO as in mapping documents only fields names are specified and these aggregate function details are not specified .So in DAO how can i retrieve these values and show them in jsp(if needed I should be able manipulate the values).
Mapping documents:<hibernate-mapping>
<class name="cl.company.dept.interfaces.bean.EmpBean" table="S_emp_table ">
<id name="empID" column="EMP_ID" type="int">
</id>
<property name="emp_movement_date" column="EMP_MVMT_DATE" type="java.util.Date" />
<property name="emp_sal_date" column="EMP_SAL_DATE" type="java.util.Date" />
<property name="savings_sal" column="SAVINGS_SALARY" type="int"/>
<property name="misc_sal" column="MISC_SALARY" type="int"/>
<property name="emp_status " column="EMP_STATUS" type="int"/>
</class>
</hibernate-mapping>
public class EmpBean {
private int empID;
private emp_movement_date;
private Date accumulatedDate;
private int emp_stat;
private int isavings_sal;
private int misc_sal;
//Getters and Setters method for the above mentioned.
}
Name and version of the database you are using: RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
The generated SQL (show_sql=true):Code:
select count(this_.empID) as y0_, sum(this_.savings_sal) as y1_, sum(this_.misc_sal) as y2_ from S_emp_table this_ where this_.emp_sal_date=? and this_.emp_status in (?, ?) and this_.emp_movement_date>?