日期:2014-05-18  浏览次数:20708 次

项目中遇到hibernate报错,求帮助!
这是我的POJO类部分代码:(当然后面是加了set和get方法的)
private String timeid;
private double change_count;
private double wlan_count;
private double data_count;
private double send_count;
private double electricy_count;
private double platform_count;
private double totle_count;

public NetWorkFaultType(){}


public NetWorkFaultType(double change_count,double wlan_count,double data_count,double send_count,double electricy_count,double platform_count,double totle_count)
{
this.change_count=change_count;
this.wlan_count=wlan_count;
this.data_count=data_count;
this.send_count=send_count;
this.electricy_count=electricy_count;
this.platform_count=platform_count;
this.totle_count=totle_count;
}

这是我写的HQL语句:
String hql="select new cn.com.starit.local.persistence.po.NetWorkFaultType(sum(t.change_count),sum(t.wlan_count)," +
"sum(t.data_count),sum(t.send_count),sum(t.electricy_count),sum(t.platform_count),sum(t.totle_count)) from NetWorkFaultType t" +
" where timeid between '"+timeList.get(0)+"' and '"+timeList.get(1)+"'";

这是我实现查询的方法:(getSession()和beginTrans()方法是在父类中实现的)
public ArrayList<NetWorkFaultType> getNetFaultType(String hql)
{
ArrayList<NetWorkFaultType> list=new ArrayList<NetWorkFaultType>();
//获取session
session=getSession();
//开启事物
beginTrans(session);
//创建query对象
query=session.createQuery(hql);
//使用query进行查询
list=(ArrayList)query.list();

return list;
}

当执行到查询的时候就会报org.hibernate.QueryException: could not instantiate: cn.com.starit.local.persistence.po.NetWorkFaultType
这个错,一直停留在页面中。(当数据库中能查到值的时候就没问题,一旦没有值就会出现)

各位,想必hibernate都有所接触,帮个忙,怎么才能解决这个错误。

------解决方案--------------------
list=(ArrayList)query.list();
错误可能是这样的:
当没有查询到数据时,某些字段的值为null,而返回结果强转为ArrayList<NetWorkFaultType>的时候NetWorkFaultType这个类中的某些属性设置了不能为空,导致创建NetWorkFaultType对象失败。
------解决方案--------------------
错误是:无法实例化这个类。我估计是你构造方法的问题,因为你的构造方法中的参数是原生类型。不从数据库中查不到结果,hibernate就无法把空的结果集封装成这个类的实例。。。如果你的构造方法中的类型不是原生的,可能这个问题就不会出现了。。。
------解决方案--------------------
引用:
Quote: 引用:

list=(ArrayList)query.list();
错误可能是这样的:
当没有查询到数据时,某些字段的值为null,而返回结果强转为ArrayList<