Java,Hibernate getList不工作
我正在尝试使用hibernate编写一个用于数据库访问的网站。保存我可以很好地工作,但是当我尝试在执行session.createQuery调用时调用我的getList方法时,代码只会进入finally方法,而不会引发异常,这让我有点困惑 代码如下:Java,Hibernate getList不工作,java,hibernate,list,Java,Hibernate,List,我正在尝试使用hibernate编写一个用于数据库访问的网站。保存我可以很好地工作,但是当我尝试在执行session.createQuery调用时调用我的getList方法时,代码只会进入finally方法,而不会引发异常,这让我有点困惑 代码如下: public List<Category> getCategories() { //insertCategory(); System.out.println("in get categories"); List&
public List<Category> getCategories() {
//insertCategory();
System.out.println("in get categories");
List<Category> result = null;
Session session = HibernateUtil.getSessionfactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Query cats = session.createQuery("from category where is_parent = 1");
result = cats.list();
transaction.commit();
for (java.util.Iterator<Category> it = result.iterator();it.hasNext();){
Category myCategory = it.next();
System.out.println(myCategory);
}
calculateBlueprintSize(result.size());
} catch (HibernateException e) {
// TODO: handle exception
transaction.rollback();
e.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
} finally {
session.close();
}
return result;
}
这是基于访问MySQL数据库
任何帮助都将不胜感激,我一直找不到任何可以帮助我的东西,而且我是全新的Hibernate新手,因此开始考虑使用原生sql和ehcache切换回DAO模式可能是最好的选择
谢谢
Matt我相信您从
createQuery
调用中得到了RuntimeException
,因为您将SQL名称与HQL名称混合在一起。我假设您的表名为category
,并且列是该表中的一个字段。如果您想使用HQL查询,您需要使用类别
实体上的属性名称,即父项
,而不是是父项
,谢谢,但即使我刚刚有:query cats=session.createQuery(“来自类别”);删除where子句仍然失败:(如果您解决了类路径问题,这个问题就会消失。
public void insertCategory() {
Category newCat = new Category();
newCat.setActive(new Integer(1));
newCat.setCategoryDescription("my test category");
newCat.setCategoryName("my cat name");
newCat.setLastUpdatedDate(new Timestamp(new Date().getTime()));
newCat.setParent(new Integer(1));
newCat.setSequence(new Integer(1));
Session session = HibernateUtil.getSessionfactory().getCurrentSession();
try {
session.beginTransaction();
// user.setUserId(new Long(2));
session.save(newCat);
session.getTransaction().commit();
} finally {
session.close();
}
}