2

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

1 Answer 1

2

I believe that you are getting a RuntimeException from the createQuery call because you are mixing SQL names with HQL names. I assume that your table is named category and that the is_parent column is a field in that table. If you want to use an HQL query, you need to use the name of the property on the Category entity, namely parent, instead of is_parent.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but even when I just have:Query cats = session.createQuery("from category"); removing the where clause it still fails :(
If you get your classpath issues taken care of, that problem will go away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.