使用hibernate填充2个下拉列表 - 一个为null

时间:2013-01-02 00:06:02

标签: hibernate

我有2个下拉列表,每个列表都填充了来自不同表的数据。我创建了2个不同的DAO类和列表方法,但第二个列表为空。

公共类GenderDAO {

private ArrayList<Gender> genders;
static final Logger logger = Logger.getLogger(GenderDAO.class);

@SuppressWarnings("unchecked")
public ArrayList<Gender> listGenders() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        genders = (ArrayList<Gender>) session.createQuery("from Gender").list();
        transaction.commit();
    } catch (HibernateException e) {
        logger.debug("In the GenderDAO HibernateException");
        transaction.rollback();
    } finally {
        logger.debug("In the GenderDAO finally block");
        session.close();
    }
    return genders;
}

公共类StateDAO {

private ArrayList<State> states;
static final Logger logger = Logger.getLogger(StateDAO.class);

@SuppressWarnings ("unchecked")
public ArrayList<State> listStates() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        states = (ArrayList<State>) session.createQuery("from State").list();
        transaction.commit();
    } catch (HibernateException e) {
        logger.debug("In the StateDAO HibernateException");
        transaction.rollback();
    } finally {
        logger.debug("In the StateDAO finally block");
        session.close();
    }
    return states;
}

}

我收到以下错误。

标记'select',字段'list',名称'state':请求的列表键'states'无法解析为collection / array / map / enumeration / iterator类型。例如:人或人。{name}

如果我在StateDAO中执行查询之前添加以下内容,则错误消失,但列表为空。

states = new ArrayList();

调用DAO方法的代码如下:

    GenderDAO genderDAO = new GenderDAO();
    genders = genderDAO.listGenders();

    StateDAO stateDAO = new StateDAO();
    states = stateDAO.listStates();

有人可以提出遗漏的内容吗?

0 个答案:

没有答案