6

如果我没有以编程方式设置任何东西,而只是调用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

根据@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

编码

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());

例外

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

概括

如果我使用全部且不使用hibernate.properties代码(代码中没有 .setProperty),一切都会很好。如果我使用部分hibernate.properties和部分代码(服务器、用户名、密码),我会在 hbm 中为每个映射属性得到错误。

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

4

3 回答 3

3

哇,刚刚解决了这个问题。

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

我错过了

.applySettings(configuration.getProperties())

学习

  1. configure() 应该在setProperty之后调用
  2. 使用hibernate.connection.url不是 connection.url如果你使用hibernate.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);    
}  
于 2013-05-13T10:00:44.703 回答
0

另一种方法是从hibernate.cfg.xmlor加载所有属性,hibernate.properties并仅以编程方式覆盖所需的属性。

Configuration config = new Configuration().configure();
config.setProperty("hibernate.connection.username", "xyz" );
config.setProperty("hibernate.connection.password", "password" ); 
于 2013-05-10T11:38:25.410 回答
0

即使您以编程方式设置连接参数(URL、用户名、密码),您也应该确保不存在其他会在应用程序启动时强制使用 JDBC 连接的条件。我至少确定了两个:

  1. hibernate.hbm2ddl.auto属性设置为“none”以外的任何其他值都会在启动时强制连接到数据库;验证、创建或更新数据库模式...

  2. 如果您在 hibernate.cfg.xml 或 persistence.xml 中使用 C3P0 属性,这也会强制池管理器在启动时尝试从数据库获取连接

在所有这些情况下,由于您在没有配置连接参数的情况下强制连接到数据库,您将收到各种错误(如您所面临的错误)......

为了修复这些错误,请禁用该属性(hibernate.hbm2ddl)或以编程方式设置其他属性(hibernate.c3p0.*

于 2014-04-04T17:10:28.333 回答