从数据库获取查询结果的问题

时间:2010-06-21 06:26:07

标签: java hibernate

我创建了一个名为SpecializationBean的类,它有两个私有字段:

private ArrayList<SelectItem> specializationItems= new ArrayList<SelectItem>();
private String specializationName;

我为这两个人提供了一个吸气剂和吸气剂。

和构建buildSpecializationList列表的specializationItems方法:(我在getter中调用此方法):

public void buildSpecializationList(){
    List<Object[]> specializations = null;

    try{
        Session mySession = HibernateUtil.getAdmSessionFactory().getCurrentSession();
        Transaction transaction = mySession.beginTransaction();
        String sql = "SELECT  J_Specialization_ID, Specialization_Name_Ar FROM J_Specialization WHERE J_Department_ID = '1000001'";
        Query query = mySession.createSQLQuery(sql).addScalar("id", Hibernate.LONG).addScalar("name", Hibernate.STRING);
        specializations = query.list();

    }
    catch(Exception e){
        e.printStackTrace();
    }
    this.specializationItems = new ArrayList<SelectItem>(90);
    for(Object[] sp: specializations ){
        this.specializationItems.add(new SelectItem(sp[0],(String) sp[1]));
    }

}

问题是我得到一个空指针异常,它表明列表specializations(在buildSpecializationList()中定义)为空。我自己在桌子上尝试了查询并返回结果。我也尝试过HQL查询(istead):

String sqlQuery = "Select JSpecializationId, specializationNameAr FROM JSpecializationWHERE JDepartmentId = '1000001'";
Query q = mySession.createQuery(sqlQuery);

但是,我仍然得到一个空指针异常,它表明查询返回null结果。你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

对于任何使用Hibernate遇到此问题的人......创建一个新类并重写整个事情可能实际上解决了问题!!!