I am attempting to write a website using hibernate for database access. Saving I can get working fine, however when I try and call my getList method upon executing the session.createQuery call the code just drops into the finally method without throwing an exception leaving me a bit confused!
Code is below:
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;
}
My insert works fine (hardcoded for now just to prove I can connect to the DB)
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();
}
}
Thi is based on accessing a MySQL database.
Any help would be appreciated, I have been unable to find anything that can help me around and I am brand new to Hibernate so beginning to thinking switching back to DAO pattern using native sql with ehcache might be the best thing to do....
Thanks
Matt