9

私はこの方法で HibernateUtil を構築します:

public class HibernateUtil {

        private static final SessionFactory sessionFactory;

        static {
            try { 
                // Create the SessionFactory from standard (hibernate.cfg.xml) config file.
                sessionFactory = new Configuration().configure().buildSessionFactory();

            } catch (Throwable ex) {
                // Log the exception. 
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }

        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
}

したがって、Eclipse の HQL エディター (Hibernate Tools を使用) で HQL コマンドを実行しようとすると、次のエラーが表示されます ここに画像の説明を入力 。ConfigureAnnotation で AnnotationConfiguration を変更すべきではありませんか?

アップデート

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password"><password></property>
        <property name="hibernate.connection.url">jdbc:mysql://<hostname>:3306/<schema></property>
        <property name="hibernate.connection.username">root</property>
        <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- SQL -->
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.show_sql">true</property>
        <!-- C3P0 -->
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">180</property>
        <property name="hibernate.c3p0.idle_test_period">100</property>
        <!-- Classes -->
        <mapping class="com.suaparte.pojo.Area" />
    </session-factory>
</hibernate-configuration>

前もって感謝します。

4

10 に答える 10

12

このエラーが発生し、hibernate バージョン >=4.0 を使用している場合、問題はおそらく Hibernate コンソールの構成にあります。

次の場所に移動してみてください:

実行 -> 実行構成

作成した構成を開き、メイン タブでタイプを Core から Annotations に変更します。ここにスクリーンショットがあります: ここに画像の説明を入力

于 2013-01-12T14:09:03.370 に答える
8

Configuration() を AnnotationConfiguration() に変更するだけです

于 2014-09-13T22:34:44.373 に答える
4

コードをから変更しました

Configuration cfg=new Configuration();
            cfg.configure("hibernate.cfg.xml");         
            SessionFactory factory=cfg.buildSessionFactory();

SessionFactory factory=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();

また、POJO クラスに「@Id」アノテーションを追加していません。「@Id」を追加した後、問題を完全に解決しました。

于 2015-05-16T07:19:18.083 に答える
3

Configuration() の代わりに AnnotationConfiguration() を使用できます

于 2015-05-03T16:00:51.343 に答える
2

次のように構築してみてください:

AnnotationConfiguration().configure().buildSessionFactory();

そのためには、次のものが必要です。

Hibernate annotations

よろしく。

宇土。

于 2011-09-19T19:15:16.900 に答える
2

hibernate ディストリビューション jar 3.6.4 バージョンをダウンロードしてみてください。jre1.6.0_07 バージョンを使用してください。これにより、 を使用する代わりに、以下のように新しい構成オブジェクトを正常に作成できますAnnotationConfiguration()

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
于 2015-12-02T23:45:08.710 に答える
-1

SessionFactoryオブジェクトを返すユーティリティ クラスを作成します。

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    static {
       try {
           sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        catch(Throwable t) {
            throw new ExceptionInInitializerError(t);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

これをメインクラスで次のように呼び出します。

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
于 2012-06-11T07:06:14.703 に答える