0

私はこのようにしようとしています:

Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

hibernateProperties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
hibernateProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + source.getHost() + "/" + source.getDataBase());
hibernateProperties.setProperty("hibernate.connection.username", source.getUsername());
hibernateProperties.setProperty("hibernate.connection.password", source.getPassword());
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");

Configuration configuration = new Configuration();
configuration.setProperties(hibernateProperties);
configuration.setProperty("packagesToScan", "com.company.comparer.entity");

SessionFactory sessionFactory = configuration.configure().buildSessionFactory(
            new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());

それは機能していません:)

4176 2012-11-28 17:48:52,583 - [main] INFO  org.hibernate.Version  - HHH000412: Hibernate Core {4.1.1}
4178 2012-11-28 17:48:52,585 - [main] INFO  org.hibernate.cfg.Environment  - HHH000206: hibernate.properties not found
4179 2012-11-28 17:48:52,586 - [main] INFO  org.hibernate.cfg.Environment  - HHH000021: Bytecode provider name : javassist
4195 2012-11-28 17:48:52,602 - [main] INFO  org.hibernate.cfg.Configuration  - HHH000043: Configuring from resource: /hibernate.cfg.xml
4195 2012-11-28 17:48:52,602 - [main] INFO  org.hibernate.cfg.Configuration  - HHH000040: Configuration resource: /hibernate.cfg.xml

私の最後の行の後、私のアプリケーションは機能から抜け出し、何もしません:)

デバッグすると、Spring によってキャッチされた次のような例外があることがわかります。

org.hibernate.HibernateException: /hibernate.cfg.xml not found

これを解決する適切な方法を教えてください。

エンティティ(javax として注釈が付けられているもの)のパッケージをスキャンしたいのですが、一部は使用hibernate.cfg.xmlせず、複数datasourcesまたはpersistence units.. 動的であるため、プログラムで実行したいだけですdatasources

4

1 に答える 1

0

私はついに私の質問に答えました:))

私は多くのデバッグを行っていましたが、エラーはスローされません (理由はわかりません)...

hibernate.cfg.xml次のようなものを作成する必要がありました。

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration >

    <session-factory>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.connection.pool_size">1</property>


        <!-- Enable Hibernate's automatic session context management -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="hibernate.show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hibernate.hbm2ddl.auto">update</property>
    </session-factory>

</hibernate-configuration>

そして、コードで私はこのように使用しました

public DBReaderImpl(DataSource source)
{
    this.source = source;


    Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

    hibernateProperties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    hibernateProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + source.getHost() + "/" + source.getDataBase());
    hibernateProperties.setProperty("hibernate.connection.username", source.getUsername());
    hibernateProperties.setProperty("hibernate.connection.password", source.getPassword());
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");

    Configuration configuration = new Configuration();
    configuration.setProperties(hibernateProperties);
    configuration.addAnnotatedClass(Route.class);

    SessionFactory sessionFactory = configuration.configure().buildSessionFactory(
            new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());

    Session session = sessionFactory.openSession();
    session.beginTransaction();

    List<Route> routes = session.createQuery("SELECT r FROM Route r").list();

    for (Route route : routes)
    {
        System.out.println(route);
    }

    session.close();
}

おそらく、リフレクションを使用してすべてのクラスを読み取り、@Entity注釈付きのすべてのクラスを構成に追加します

于 2012-11-29T08:58:31.923 に答える