Java Hibernate在本地计算机上工作,但在Heroku上失败

Java Hibernate在本地计算机上工作,但在Heroku上失败,java,mysql,hibernate,spring-mvc,heroku,Java,Mysql,Hibernate,Spring Mvc,Heroku,因此,我最近在Heroku上部署了我的应用程序,但当我尝试将hibernate连接添加到数据库时,它失败了。我将JawsDB作为附加组件添加到heroku应用程序中,并使用以下hibernate.cfg.xml连接到它: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD

因此,我最近在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>
然后,我将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,但不幸的是,我无法找到适合我的案例的教程。如果你想要更多的信息,我非常乐意提供。如果您能为我指出正确的方向,我将不胜感激。

您应该将您的cfg文件放在
src/main/resources
中,或者您可以这样告诉我配置cfg文件的绝对路径:

configuration.configure(“/*{PATH-TO-YOUR-FILE}*/hibernate.cfg.xml”);

(第一个更好。)

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

你使用过时的配置而不是Spring Boot和Hibernate JPA个性有什么特别的原因吗?我之所以使用它,是因为在关于Spring MVC和Hibernate的课程中,他们向我展示了如何这样配置它,而且它一直在工作。我应该使用Spring Boot和Hibernate JPA吗Hibernate JPA?那是我的建议。看起来您正在使用针对Hibernate 3的资源;目前的版本是5.3。使用SpringBoot,您唯一需要做的就是提供JDBC连接URL(包括或单独提供用户名/密码),如果它绑定到应用程序,Boot可以从Heroku和CloudFoundry自动检测到它。此设置的其余部分将自动完成。(如果您有选择的话,最好使用JPA API、Spring
@Transactional
和Spring Data JPA。)您能告诉我一个关于如何迁移或设置它的好教程吗?我不确定我是否理解您将cfg文件放在resources文件夹中的意思。如果我将其移动到参考资料中,我将如何引用此文件以获取SessionFactory。如果您无法更改文件位置,则可以在调用
Configure
时设置cfg文件的路径(如我在回答中的第二种方式所述)。谢谢!遇到同样的问题,您的解决方案仍然有效。
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