首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring 3.1不带persistence.xml的hibernate 4-多数据源(无web应用程序)

spring 3.1不带persistence.xml的hibernate 4-多数据源(无web应用程序)
EN

Stack Overflow用户
提问于 2012-11-28 23:53:47
回答 1查看 648关注 0票数 0

我试着这样做:

代码语言:javascript
复制
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());

它就是不起作用:)

代码语言:javascript
复制
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捕获了一个异常,它说:

代码语言:javascript
复制
org.hibernate.HibernateException: /hibernate.cfg.xml not found

你能告诉我解决这个问题的正确方法是什么吗?

我想扫描包中的实体(注释为javax...)不使用某些hibernate.cfg.xml,也不想使用多个datasourcespersistence units。我只想通过编程来完成这项工作,因为我有动态datasources

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-29 16:58:32

我终于回答了我的问题:))

我调试了很多,错误没有抛出(我不知道为什么)……

我必须创建一个如下所示的hibernate.cfg.xml

代码语言:javascript
复制
<?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>

然后在我使用的代码中,如下所示

代码语言:javascript
复制
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类,以便将所有带注释的类添加到我的配置中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13609111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档