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,我不想使用多个datasourcespersistence 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 回答