hibernate在本地机器上工作,但在heroku上失败

fzwojiic  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(290)

因此,我最近在heroku上部署了我的应用程序,但是当我尝试将hibernate连接添加到数据库时失败了。我将jawsdb作为附加组件添加到heroku应用程序中,并使用以下hibernate.cfg.xml连接到它:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://ehc1u4pmphj917qf.cbetxkdyhwsb.us-east-1.rds.amazonaws.com:3306/ycm07x7z21h40k84</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.default_schema">ycm07x7z21h40k84</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
        <property name="hibernate.connection.CharSet">utf8</property>
        <property name="hibernate.connection.characterEncoding">utf8</property>
        <property name="hibernate.connection.useUnicode">true</property>
    </session-factory>
</hibernate-configuration>

我的hibernate配置类如下所示:

public class HibernateUtils {
  private static SessionFactory sessionFactory;

  static {
    try {
      Configuration configuration = new Configuration().configure();

      configuration.addAnnotatedClass(City.class);
      ...
      configuration.addAnnotatedClass(Review.class);

      StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
              .applySettings(
                      configuration.getProperties())
              .build();

      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (Exception ex) {
      System.out.println("----------------------");
      System.out.println(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}

之后,我将sessionfactory作为bean进行连接,并在需要它的地方将其作为依赖项提供。当我编译并在本地运行它时,一切都正常,但当我将它部署到heroku时,会出现以下错误:

2018-12-11T18:52:35.687631+00:00 app[web.1]: ----------------------
2018-12-11T18:52:35.687723+00:00 app[web.1]: org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
2018-12-11T18:52:35.692621+00:00 app[web.1]: 2018-12-11 18:52:35.692  WARN 4 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'usersService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dbProfileService' defined in URL [jar:file:/app/build/libs/build_3eb7c290daa3670885240cb93ab898a5-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/squadknowhow/services/DbProfileService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'provideUsersGenericRepository' defined in class path resource [squadknowhow/configurations/AppConfig.class]: Unsatisfied dependency expressed through method 'provideUsersGenericRepository' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2018-12-11T18:52:35.696062+00:00 app[web.1]: 2018-12-11 18:52:35.695  INFO 4 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-12-11T18:52:36.049747+00:00 app[web.1]: 2018-12-11 18:52:36.049  WARN 4 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
2018-12-11T18:52:36.539830+00:00 heroku[web.1]: Process exited with status 1
2018-12-11T18:52:36.557418+00:00 heroku[web.1]: State changed from starting to crashed

我绝对确信用户名和密码是有效的。我用mysql workbench成功地连接到了数据库。我怀疑为了在heroku上使用hibernate,我必须另外配置hibernate,但不幸的是,我找不到一个关于我的案例的教程。如果你想要更多的信息,我非常乐意提供。如果你能给我指出正确的方向,我也会非常感激。

zmeyuzjn

zmeyuzjn1#

你应该把你的cfg文件放在 src/main/resources 或者您可以这样告诉配置cfg文件的绝对路径: configuration.configure("/*{PATH-TO-YOUR-FILE}*/hibernate.cfg.xml"); (第一个更好。)

ggazkfy8

ggazkfy82#

基本上,问题是heroku找不到hibernate.cfg.xml文件,但同时配置java文件需要它。解决方案是在没有hibernate.cfg.xml文件的情况下配置hibernate,我在这个网站上找到了该如何做:https://www.concretepage.com/hibernate/configure_hibernate_without_hibernate_cfg_xml

相关问题