【问题标题】:spring 3.1 hibernate 4 without persistence.xml - multiple datasources (no web application)没有persistence.xml的spring 3.1 hibernate 4 - 多个数据源(没有Web应用程序)
【发布时间】:2012-11-16 13:01:14
【问题描述】:

我正在尝试这样:

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

【问题讨论】:

    标签: java spring hibernate orm sessionfactory


    【解决方案1】:

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

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

    我必须创建一个看起来像这样的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 类,以将所有带注释的类添加到我的配置中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-11
      • 2011-02-08
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2013-07-20
      • 2020-01-26
      相关资源
      最近更新 更多