2

データベースへのアクセスに休止状態を使用して Web サイトを作成しようとしています。保存すると正常に動作しますが、session.createQuery 呼び出しの実行時に getList メソッドを呼び出そうとすると、コードは例外をスローせずに finally メソッドにドロップされ、少し混乱します!

コードは以下のとおりです。

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;
}

私の挿入は正常に機能します(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();
    }

}

これは、MySQL データベースへのアクセスに基づいています。

何か助けていただければ幸いです。私を助けることができるものを見つけることができず、Hibernate を初めて使用するので、ehcache でネイティブ SQL を使用して DAO パターンに戻すことを考え始めるのが最善の方法かもしれません..

ありがとう

マット

4

1 に答える 1

2

SQL名とHQL名を混在させているためRuntimeException、呼び出しからを取得していると思います。テーブルに名前が付けられており、 列がそのテーブルのフィールドであるcreateQueryと想定しています。HQLクエリを使用する場合は、エンティティのプロパティの名前、つまり、の代わりにを使用する必要があります。categoryis_parentCategoryparentis_parent

于 2012-04-11T21:01:39.307 に答える