我有以下表格模式的表格:
我创建了一个方法,返回查询执行的值列表。
protected List findAllWithGroupClause(Class clazz) {
List objects = null;
try {
startOperation();
String SQL_QUERY = "SELECT COUNT(serviceName) AS RunningInstances ,serviceName,SUM(numberofthread) as workerThread "
+ "FROM servicemanagerdetails WHERE servicemanagerstatus=:status GROUP BY serviceName";
Query query = session.createQuery(SQL_QUERY);
query.setString("status", "Running");
objects = query.list();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return objects;
}
并声明了ServiceManagerDetails.hbm.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.arosys.hibernatedatamanager.ServiceManagerDetails" table="servicemanagerdetails">
<id column="servicedetailsid" name="servicedetailsID" type="integer">
<generator class="increment"/>
</id>
<property column="serviceID" name="servicemanagerID" type="string"/>
<property column="servicemode" name="servicemode" type="string"/>
<property column="servicetype" name="servicetype" type="string"/>
<property column="servicemanagerstatus" name="servicemanagerstatus" type="string"/>
<property column="controlqueue" name="controlQueue" type="string"/>
<property column="controlexchange" name="contolExchange" type="string"/>
<property column="controlroutingkey" name="controlRoutingkey" type="string"/>
</class>
</hibernate-mapping>
我创建了列别名,因为有必要编写setter和getter方法以及定义映射吗?请解释如何从列表中检索值。
由于
答案 0 :(得分:1)
你有一个select语句用于选择聚合函数COUNT,SUM等。在这种情况下,hibernate Query.list()
返回一个对象数组列表。
如果你想获得第四行的SUM,你可以
Object obj4[] = objects.get(3);
long sum4 = ((Number)obj4[2]).longValue();
以及您执行的第三行的serviceName
Object obj3[] = objects.get(2);
String name2 = (String)obj4[2];
(这两个例子都用来解释;你的代码看起来会更加变化)。
你不需要写getter和setter,你甚至不能这样做。您不能在选择字符串之外使用列别名。