IceCream Sandwitch
IceCream Sandwitch

Reputation: 285

Hibernate Native SQL Query

I have my Native SQL statement like this:

SQLQuery:

select Name ,Id,COUNT(ID) from Employee;

Employee HBM file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="edu.model.Master" table="test_prefixmaster">
        <id name="empcode" column="EMPCODE" length="10" ></id>

        <property name="empname">
            <column name="NAME" length="30" />
        </property>
    </class>
    <sql-query name="SQLQuery" callable="true">
    <return alias="emp" class="edu.model.Employee">
        <return-property name="empid" column="EMPID" />
        <return-property name="empname" column="EMPNAME" />
        </return>
        </sql-query>
</hibernate-mapping>

In MyTest Class:

   Query query = session.getNamedQuery("SQLQuery");
    List emp=new ArrayList();
    for (int i = 0; i < emp.size(); i++) {                  
    Employee emp = (Employee) emp.get(i);
    System.out.println("Employee Id:::" + emp.getEmpId());
   System.out.println("Employee Name:::" + emp.getEmpname());
//I want to get here System.out.println("Employee Count");
}

How can I map and print the Employee count also in the loop?

Upvotes: 0

Views: 2348

Answers (2)

palayan
palayan

Reputation: 59

The answer you've chosen as the best answer serves your purpose correctly. But if you want the count of the ID from the query you need to add another property in your entity class as empIdCount and map the count value to it. This will slove your purpose. You can directly get the count of the EmpIds.

Upvotes: 0

NPKR
NPKR

Reputation: 5496

   Query query = session.getNamedQuery("SQLQuery");
    List empList=new ArrayList();
    for (int i = 0; i < empList.size(); i++) {                  
    Employee emp = (Employee) empList.get(i);
    System.out.println("Employee Code:::" + emp.getEmpcode());
   System.out.println("Employee Name:::" + emp.getEmpname());

//I want to get here System.out.println("Employee Count");
   System.out.println("Employee Count:::" + empList.size());
}

for Distinct Employee

change your query

select Name ,distinct (Id) from Employee ;

Upvotes: 1

Related Questions