我仍然是 Hibernate 的新手,正在尝试从简单的 SELECT 查询中检索结果。但是,我不断收到 ClassCastException。谁能告诉我我在这里做错了什么?
这是代码:
public Wo getWoById(int id) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Wo> result = (List<Wo>) session.createQuery("from Wo where woid = " + id);
if (result!=null && result.size()==1)
return result.get(0);
else return null;
}
...以及错误消息:
Exception in thread "main" java.lang.ClassCastException:
org.hibernate.internal.QueryImpl cannot be cast to java.util.List
at implDAO.WoImplDAO.getWoById(WoImplDAO.java:16)
at logic.Logic.deleteWo(Logic.java:72)
at nl.hanze.funda.admin.main.Main.<init>(Main.java:20)
at nl.hanze.funda.admin.main.Runner.main(Runner.java:16)