Hibernate程序配置无法正常工作

时间:2016-08-23 00:07:10

标签: java mysql hibernate jdbc orm

当我使用hibernate.properties文件时,我可以从Configuration对象成功创建一个SessionFactory:

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

然而,我想使用typesafe配置库而不是hibernate.properties,以便能够将配置作为环境变量传递,因此我删除了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-core 5.2.2.Final和hibernate-annotations 3.5.6-Final和mysql-connection-java 6.0.3并使用Guice进行依赖注入,而不使用Spring。

1 个答案:

答案 0 :(得分:0)

我在错误中注意到了这一点:

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

我可以通过删除hibernate-annotations依赖来解决问题,因为hibernate-core 5.2已经包含了它。