UnsupportedOperationException:应用程序必须提供JDBC连接

Sid*_*rth 6 hibernate hibernate-mapping

如果我没有以编程方式设置任何内容,只需调用Configuration configuration = new Configuration().configure();并使用hibernate.properties(如下所示),一切都很有效.一旦我尝试以编程方式提供用户名,密码和连接URL,我就会得到一个奇怪的异常,暗示hbm文件.我错过了什么?

这个作品

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://myEC2/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.username=root
hibernate.connection.password=mypwd
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.c3p0.idle_test_period=300
hibernate.c3p0.timeout=120
Run Code Online (Sandbox Code Playgroud)

根据@Kshitij推荐.做混合模式.

现在是hibernate.properties

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
Run Code Online (Sandbox Code Playgroud)

代码

String connection = "jdbc:mysql://"
            + Globals.DBSERVER
            + "/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
        Configuration configuration = new Configuration()   
            .setProperty("hibernate.connection.url", connection)                                
            .setProperty("hibernate.connection.username", Globals.DB_USER_NAME)     
            .setProperty("hibernate.connection.password", Globals.DB_PASSWORD);
        configuration.configure();

        sessionFactory = configuration
                .buildSessionFactory(new ServiceRegistryBuilder()
            .buildServiceRegistry());
Run Code Online (Sandbox Code Playgroud)

例外

我现在得到这个异常,一个用于mapping resource我的hbm文件中的每个条目.

11 May 2013 08:46:31,969 1300 [main] FATAL ReadOnlyOperations  - Have chosen to ignore this runtime exception java.lang.UnsupportedOperationException: The application must supply JDBC connections, may be fatal, examine this carefully
11 May 2013 08:46:31,969 1300 [main] FATAL ReadOnlyOperations  - java.lang.UnsupportedOperationException: The application must supply JDBC connections
Run Code Online (Sandbox Code Playgroud)

摘要

如果我使用all hibernate.properties而没有代码(代码中没有.setProperty),一切都很好.如果我使用零件hibernate.properties和零件代码(服务器,用户名,密码),我会在hbm中为每个映射属性获取错误.

我需要有人帮我弄清楚我错过了什么.它应该是非常基本的东西.

Sid*_*rth 3

哇,刚刚解决了问题。

sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());
Run Code Online (Sandbox Code Playgroud)

我错过了

.applySettings(configuration.getProperties())

学习内容

  1. configure() 应该在setProperty之后调用
  2. 使用,如果使用hibernate.connection.url则不使用 connection.urlhibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
  3. 将hibernate日志的log4j属性设置为ALL,以便您可以看到更详细的问题
  4. 要摆脱WARN Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!您还需要替换http://www.hibernate.org/dtd/cfg.xml和所有hbm文件。不要忘记hbm文件,它们也使用相同的 DTD。

最后提到这个,解决这个问题。最后的建议Bill Gorder非常棒。

private static SessionFactory configureSessionFactory()    
        throws HibernateException {    
    Configuration configuration = new Configuration();    
    configuration.configure();    
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()    
            .applySettings(configuration.getProperties())    
            .buildServiceRegistry();    
    return configuration.buildSessionFactory(serviceRegistry);    
}  
Run Code Online (Sandbox Code Playgroud)