2013-05-10 80 views
5

如果我沒有以編程方式設置任何東西,只是調用Configuration configuration = new Configuration().configure();並使用hibernate.properties(如下所示),一切都很好。只要我嘗試以編程方式提供用戶名,密碼和連接URL,我就會得到一個奇怪的異常,暗示着hbm文件。我錯過了什麼?UnsupportedOperationException:應用程序必須提供JDBC連接

這個作品

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中收到錯誤。

我需要有人幫助我找出我失蹤的東西。它應該是非常基本的東西。

+0

[請參見參考資料:http://stackoverflow.com/questions/11211451/connection-cannot-be-null-when-hibernate-dialect-not -set) – gks 2013-05-10 11:23:56

+0

我已經試過了,不起作用。 – Siddharth 2013-05-10 11:27:56

回答

3

哇,剛解決問題。

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

我錯過了

.applySettings(configuration.getProperties())

學習收穫

  1. 配置()應該叫的setProperty
  2. 使用hibernate.connection.urlconnection.url如果使用hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
  3. 集的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!的,你需要在cfg中替換http://www.hibernate.org/dtd/。xml和全部hbm文件。不要忘記hbm文件,他們也使用相同的DTD。

最後,提到這個,修復thisBill 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);  
} 
+0

所以你是混合屬性還是添加所有內部的java文件? – Kshitij 2013-05-13 10:30:17

+0

混合,一些在屬性文件,和一些在java – Siddharth 2013-05-13 10:53:37

+0

我也有同樣的問題..我無法找到類'ServiceRegistryBuilder()'它將工作的版本的HIbernate。我正在使用Hibernate 3.x – Babel 2014-06-30 08:42:11

0

另一種方法是加載hibernate.cfg.xmlhibernate.properties中的所有屬性,並僅以編程方式覆蓋所需屬性。

Configuration config = new Configuration().configure(); 
config.setProperty("hibernate.connection.username", "xyz"); 
config.setProperty("hibernate.connection.password", "password"); 
+0

我現在正在'2013年5月10日14:20:34,238 1196 [main]致命ReadOnlyOperations - 選擇忽略此運行時異常java.lang.UnsupportedOperationException:應用程序必須提供JDBC連接,可能是致命的,請仔細檢查 '每個HBM文件1個。 – Siddharth 2013-05-10 14:22:40

+0

基本上,在我做出更改後,非hbm文件正在加載。所以它仍然不完整。 – Siddharth 2013-05-10 16:20:21

+0

你有沒有設置'hibernate.connection.url'?從你修改過的代碼看來,你似乎沒有用編程的方式。 – Kshitij 2013-05-12 07:50:35

0

即使你設置連接參數(URL,用戶名,密碼)編程,你應該確保其他條件,這將迫使JDBC連接的使用應用程序啓動時不存在。我確定至少有兩個:

  1. 設置hibernate.hbm2ddl.auto屬性不是「無」的任何其他值強制在啓動時數據庫的連接;驗證,創建或更新數據庫架構...

  2. 如果您在您的hibernate.cfg.xml或persistence.xml中使用C3P0屬性,這也會強制池管理器試圖從數據庫獲取連接啓動

在所有這些情況下,因爲你被逼到數據庫的連接也沒有配置連接參數,你會收到各種錯誤(如你所面臨的一個)...

爲了解決這些錯誤要麼禁用屬性(hibernate.hbm2ddl)o r也編程設置其他屬性(hibernate.c3p0。*

相關問題