我有 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;
}
}
我收到以下错误。
标记“选择”、字段“列表”、名称“状态”:请求的列表键“状态”无法解析为集合/数组/映射/枚举/迭代器类型。示例:人或人。{name}
如果我在 StateDAO 中执行查询之前添加以下内容,则错误消失但列表为空。
状态 = 新的 ArrayList();
调用DAO方法的代码如下:
GenderDAO genderDAO = new GenderDAO();
genders = genderDAO.listGenders();
StateDAO stateDAO = new StateDAO();
states = stateDAO.listStates();
有人可以建议缺少什么吗?