package com.labmanager.fw.JUNIT;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.labmanager.fw.data.bo.Lab;
import com.labmanager.fw.util.HibernateSessionFactory;
public class PageDeviceTest{
public List getResultCount(String table){
Session session=HibernateSessionFactory.getSession();
Transaction ts=session.beginTransaction();
List list=new ArrayList();
String sql="select count(*) from "+table+" where id > 0";
try{
ts.begin();
SQLQuery sqlQuery=session.createSQLQuery(sql);
list=sqlQuery.list();
ts.commit();
System.out.println("查询到数据"+list.get(0).toString()+"条");
}catch(Exception e){
ts.rollback();
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return list;
}
public void dispaly(){
List list=new ArrayList();
list=this.getResultCount("tblab");
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(((Lab)it.next()).getName());
}
}
public static void main(String[] args){
new PageDeviceTest().dispaly();
}
}
&
Console输出:
查询到数据20条
Exception in thread "main" java.lang.ClassCastException: java.math.BigInteger cannot be cast to com.labmanager.fw.data.bo.Lab
at com.labmanager.fw.JUNIT.PageDeviceTest.dispaly(PageDeviceTest.java:40)
at com.labmanager.fw.JUNIT.PageDeviceTest.main(PageDeviceTest.java:44)
&
Lab类是Hibernate inverse 生成的。
主键类型为Short
配置文件如下
<hibernate-mapping>
<class name="com.labmanager.fw.data.bo.Lab" table="tblab" catalog="labmanager">
<id name="id" type="java.lang.Short">
<column name="id" />
<generator class="identity"></generator>
</id>
<many-to-one name="currriculum" class="com.labmanager.fw.data.bo.Currriculum" fetch="select">
<column name="curriculumId" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="profile" type="java.lang.String">
<column name="profile" />
</property>
<property name="deviceNumbers" type="java.lang.Short">
<column name="deviceNumbers" />