【问题标题】:Hibernate programmatic configuration not working休眠编程配置不起作用
【发布时间】:2016-12-29 15:35:18
【问题描述】:

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

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

但是,我想使用类型安全配置库而不是 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。

【问题讨论】:

  • 你在用@Configuration吗?
  • 不确定我应该在哪里使用它,我使用 Guice 进行依赖注入,而不是 Spring
  • 对不起,我只用过spring。

标签: java mysql hibernate jdbc orm


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 2013-10-16
    相关资源
    最近更新 更多