0

我有这样的 Native SQL 语句:

SQL查询:

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

员工 HBM 文件:

<?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>

在 MyTest 类中:

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

如何在循环中映射和打印员工人数?

4

2 回答 2

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

对于不同的员工

更改您的查询

select Name ,distinct (Id) from Employee ;
于 2013-01-09T12:21:32.443 回答
0

您选择的最佳答案正确地符合您的目的。但是,如果您想要查询中的 ID 计数,则需要在实体类中添加另一个属性作为 empIdCount 并将计数值映射到它。这将满足您的目的。您可以直接获取 EmpIds 的计数。

于 2013-07-17T09:44:35.600 回答