2016-08-23 45 views
0

當我使用hibernate.properties文件,我可以成功地從我的配置對象創建一個會話:Hibernate的編程配置不工作

Configuration configuration = new Configuration() 
       .addAnnotatedClass(WeatherResponse.class); 
SessionFactory sessionFactory = configuration.buildSessionFactory(); 

代替hibernate.properties不過,我想使用類型安全的配置庫中爲了能夠通過CONFIGS作爲環境變量,所以我刪除hibernate.properties和編程設置屬性如下:

Configuration configuration = new Configuration() 
       .addAnnotatedClass(WeatherResponse.class) 
       .setProperty("hibernate.connection.username", config.getString("user")) 
       .setProperty("hibernate.connection.password", config.getString("pass")) 
       .setProperty("hibernate.connection.driver_class", config.getString("driver")) 
       .setProperty("hibernate.connection.url", config.getString("url")) 
       .setProperty("hibernate.dialect", config.getString("dialect")); 
SessionFactory sessionFactory = configuration.buildSessionFactory(); 

但即使我設置的所有屬性,我在我的屬性文件,我得到錯誤:

16:49:55.338 [qtp550572371-22] INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found 
16:49:55.340 [qtp550572371-22] INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist 
16:49:55.379 [qtp550572371-22] INFO o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
16:49:55.486 [qtp550572371-22] WARN o.h.e.j.c.i.ConnectionProviderInitiator - HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections 
16:49:55.487 [qtp550572371-22] WARN o.h.e.j.e.i.JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections 
16:49:55.495 [qtp550572371-22] WARN o.e.jetty.servlet.ServletHandler - /weather/new+york 
org.jboss.resteasy.spi.UnhandledException: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 

我使用Hibernate的核心5.2.2.Final和休眠的註解3.5.6決賽和MySQL連接的Java 6.0.3和使用吉斯依賴注入,我不使用Spring。

+0

你在使用'@ Configuration'嗎? –

+0

不知道我應該在哪裏使用它,我使用Guice進行依賴注入,而不是Spring – iman

+0

對不起,我只用它與春天。 –

回答

0

我在錯誤注意到了這一點:

java.lang.ClassNotFoundException: org.hibernate.cfg.Mappings 

,我可以通過自休眠核5.2已經包括它刪除休眠的註解依賴性解決問題。