Spring Boot 2.2 多模块项目自动配置 JPA 失败

huangapple go评论93阅读模式
英文:

Spring Boot 2.2 Multi Module Project auto configuration JPA failed

问题

I have a Spring-Boot multimodule Project.
Main Module is a Vaadin Web-App (at the Moment very simple) and a second Module called Login with Spring Entitys, Repository and Service bean.

In the Login Module I have written Testcode that runs against a H2 Database. Tests are green, Entity was created, repository exists and so on. (Everything fine)

When I try to start the WebApp I always get the Message

A component required a bean named 'entityManagerFactory' that could not be found.

My Main Application Class

  1. @SpringBootApplication(scanBasePackages = "eu.dwich.btc", exclude = ErrorMvcAutoConfiguration.class)
  2. @Import(BtcBackendLoginConfig.class)
  3. public class Application extends SpringBootServletInitializer {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

Then my Config Class in the module

  1. @SpringBootConfiguration
  2. @EnableAutoConfiguration
  3. @ComponentScan(basePackageClasses = BtcBackendLoginConfig.class)
  4. @EntityScan(basePackageClasses = BtcBackendLoginConfig.class)
  5. @EnableJpaRepositories(basePackageClasses = BtcBackendLoginConfig.class)
  6. public class BtcBackendLoginConfig {
  7. }

and the properties file

  1. database.driver-class-name=org.postgresql.Driver
  2. database.database-url=jdbc:postgresql://localhost:5433/btc_dev
  3. database.database-user=btc_dev
  4. database.database-user-password=xxxxx
  5. # --------------------------------------
  6. # special DB settings
  7. database.hibernater-format-s-q-l=true
  8. database.hibernater-gen-statistics=true
  9. database.hibernate-dialect=org.hibernate.dialect.PostgreSQL9Dialect
  10. # possible: create-drop, none, validate, update, create
  11. database.hibernate-ddl-auto=create-drop
  12. database.create-db-script=true

First (maybe simple) Question, where should I put the application.properties to configure the database, in the web Project or in the Login module?

Second Question what I'm doing wrong? is it not possible to get the spring boot auto-configuration to work? (I don't want to configure datasource and entity manager)

I have tried a lot of different configurations nothing worked until now.

Thanks in advance

Update 1:
there is only one line with a warning, no other stack trace

  1. 2020-09-24 08:07:34.788 INFO 19699 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/h2-console/**'], []
  2. 2020-09-24 08:07:34.802 INFO 19699 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@b246c96, org.springframework.security.web.context.SecurityContextPersistenceFilter@f632a44, org.springframework.security.web.header.HeaderWriterFilter@3bd390ec, org.springframework.security.web.authentication.logout.LogoutFilter@44e09d5a, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@30d21b64, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1eed7157, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1d1c15ef, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@15a14e1e, org.springframework.security.web.session.SessionManagementFilter@6a47a0c5, org.springframework.security.web.access.ExceptionTranslationFilter@5492fd4, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4f5c34f5]
  3. 2020-09-24 08:07:34.825 WARN 19699 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - canceling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userGroupRepository': Cannot create inner bean '(inner bean)#542c45c7' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#542c45c7': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
  4. 2020-09-24 08:07:34.827 INFO 19699 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]

At the end there is only this message, is it possible to get more information?

  1. 2020-09-24 08:07:34.887 ERROR 19699 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
  2. ***************************
  3. APPLICATION FAILED TO START
  4. ***************************
  5. Description:
  6. A component required a bean named 'entityManagerFactory' that could not be found.
  7. Action:
  8. Consider defining a bean named 'entityManagerFactory' in your configuration.
  9. Process finished with exit code 0

Update:
This output will be generated when turning debug on:

  1. Exclusions:
  2. -----------
  3. org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  4. org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
  5. org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
  6. org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  7. org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
  8. org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
  9. org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
  10. org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
  11. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  12. at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  13. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  14. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  15. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  16. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  17. at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:680) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  18. at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:498) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
  19. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowire
  20. <details>
  21. <summary>英文:</summary>
  22. I have a Spring-Boot multimodule Project.
  23. Main Module is a Vaadin Web-App (at the Moment very simple) and a second Module called Login with Spring Entitys, Repository and Service bean.
  24. In the Login Module I have written Testcode that runs against a H2 Database. Tests are green, Entity was created, repository exists and so on. (Everything fine)
  25. When I try to start the WebApp I allways get the Message
  26. &gt; A component required a bean named &#39;entityManagerFactory&#39; that could not be found.
  27. My Main Applicattion Class

@SpringBootApplication(scanBasePackages = "eu.dwich.btc", exclude = ErrorMvcAutoConfiguration.class)
@Import(BtcBackendLoginConfig.class)
public class Application extends SpringBootServletInitializer {

  1. public static void main(String[] args) {
  2. SpringApplication.run(Application.class, args);
  3. }

}

  1. Then my Config Class in the module

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = BtcBackendLoginConfig.class)
@EntityScan(basePackageClasses = BtcBackendLoginConfig.class)
@EnableJpaRepositories(basePackageClasses = BtcBackendLoginConfig.class)
public class BtcBackendLoginConfig {
}

  1. and the properties file

database.driver-class-name=org.postgresql.Driver
database.database-url=jdbc:postgresql://localhost:5433/btc_dev
database.database-user=btc_dev
database.database-user-password=xxxxx

--------------------------------------

special DB settings

database.hibernater-format-s-q-l=true
database.hibernater-gen-statistics=true
database.hibernate-dialect=org.hibernate.dialect.PostgreSQL9Dialect

possible: create-drop, none, validate, update, create

database.hibernate-ddl-auto=create-drop
database.create-db-script=true

  1. First (maybe simple) Question, where should I put the application.properties to configure the database, in the web Project or in the Login module?
  2. Second Question what I&#39;m doing wrong? is it not possible to get the spring boot autoconfiguration to work? (I don&#39;t whant to configure datasource and entitymanager)
  3. I have tryed a lot of different configrurations nothing worked until now.
  4. Thanks in advance
  5. Update 1:
  6. there is only one line with a warning, no other stacktrace

2020-09-24 08:07:34.788 INFO 19699 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/h2-console/**'], []
2020-09-24 08:07:34.802 INFO 19699 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@b246c96, org.springframework.security.web.context.SecurityContextPersistenceFilter@f632a44, org.springframework.security.web.header.HeaderWriterFilter@3bd390ec, org.springframework.security.web.authentication.logout.LogoutFilter@44e09d5a, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@30d21b64, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1eed7157, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1d1c15ef, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@15a14e1e, org.springframework.security.web.session.SessionManagementFilter@6a47a0c5, org.springframework.security.web.access.ExceptionTranslationFilter@5492fd4, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4f5c34f5]
2020-09-24 08:07:34.825 WARN 19699 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userGroupRepository': Cannot create inner bean '(inner bean)#542c45c7' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#542c45c7': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2020-09-24 08:07:34.827 INFO 19699 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]

  1. At the end ther is only this message, is it possible to get more information?

2020-09-24 08:07:34.887 ERROR 19699 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

A component required a bean named 'entityManagerFactory' that could not be found.

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

Process finished with exit code 0

  1. Update:
  2. This output will be generated when turning debug on:

Exclusions:

  1. org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  2. org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
  3. org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
  4. org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  5. org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
  6. org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
  7. org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:680) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:498) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:374) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:134) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1451) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:860) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at eu.dwich.btc.Application.main(Application.java:24) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.0.RELEASE.jar:2.2.0.RELEASE]

  1. why does spring exclude the whole spring Data stack??
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 你使用了错误的变量键,因此Spring无法配置所需的对象。
  6. 改为使用以下内容:
  7. ```properties
  8. spring.datasource.driver-class-name=org.postgresql.Driver
  9. spring.datasource.url=jdbc:postgresql://localhost:5433/btc_dev
  10. spring.datasource.username=btc_dev
  11. spring.datasource.password=xxxxx
  12. spring.jpa.show-sql=true
  13. spring.jpa.properties.hibernate.format-sql=true
  14. spring.jpa.properties.hibernate.generate_statistics=true
  15. spring.jpa.database=postgresql
  16. spring.jpa.hibernate.dll-auto=create-drop

另外,请确保类路径上有所有所需的库。您至少需要 javax.persistence-apihibernate

我还建议您不要使用 basePackageClasses,除非您知道它的作用。如果您将应用程序配置放在其自己的包中而不是根目录下,Spring将无法找到任何符合条件的类。
至少目前,请改为使用 basePackages = "eu.dwich.btc"

这里有一个常见的Spring属性列表。
在 # DATASOURCE 部分旁边,您会看到对 DataSourceAutoConfiguration 的引用。

英文:

You use the wrong variable keys so Spring cannot configure the objects required.

Instead, use the following

  1. spring.datasource.driver-class-name=org.postgresql.Driver
  2. spring.datasource.url=jdbc:postgresql://localhost:5433/btc_dev
  3. spring.datasource.username=btc_dev
  4. spring.datasource.password=xxxxx
  5. spring.jpa.show-sql=true
  6. spring.jpa.properties.hibernate.format-sql=true
  7. spring.jpa.properties.hibernate.generate_statistics=true
  8. spring.jpa.database=postgresql
  9. spring.jpa.hibernate.dll-auto=create-drop

Also, make sure you have all the required libraries on the classpath.
You need at least javax.persistence-api and hibernate.

I would also advice you NOT to use basePackageClasses unless you know what it does. If you place your Application configuration in his own package instead of the root, Spring won't be able to find any eligible classes.
At least for now, use basePackages = &quot;eu.dwich.btc&quot; instead.

Here is a list of common Spring properties.
Next to the # DATASOURCE section you see the reference to DataSourceAutoConfiguration.

答案2

得分: 0

检查您的pom文件,您必须缺少一些依赖项。我之前也遇到过相同的问题,在仔细查看了pom文件后解决了,缺少了PostgreSQL驱动jar包和spring-data依赖。它会显示非常常见的错误,您应该启用堆栈跟踪
"server.error.include-stacktrace=never"
以获取更详细的错误信息。

英文:

Check your pom file , you must be missing some dependencies. I was also same problem and resolved after carefully gone through pom file , posgress driver jar was missing and spring-data depenndency was missing. Its giving very generic error you should enable stack trace
"server.error.include-stacktrace=never"
to get more detailed error.

答案3

得分: -1

我认为我找到了错误。

  1. 简单的Spring应用程序,没有导入
  1. @SpringBootApplication(scanBasePackages = "eu.dwich.btc", exclude = ErrorMvcAutoConfiguration.class)
  2. public class Application extends SpringBootServletInitializer {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class, args);
  5. }
  6. }
  1. 后端配置需要一些修改
  1. @Configuration
  2. @EnableTransactionManagement
  3. @EntityScan(basePackages = "eu.dwich.btc")
  4. @EnableJpaRepositories(basePackages = "eu.dwich.btc")
  5. @Import({
  6. DataSourceAutoConfiguration.class,
  7. HibernateJpaAutoConfiguration.class,
  8. DataSourceTransactionManagerAutoConfiguration.class })
  9. @Profile("!test")
  10. public class BtcBackendLoginConfig {
  11. }

第二个 @SpringBootApplication 是一个错误,不需要它。
没有自动配置导入,它将无法工作。

并且要小心属性,就是这样......

  1. #spring.jpa.properties.hibernate.ddl-auto=create-drop 是错误的
  2. #spring.jpa.hibernate.hbm2ddl.auto=create-drop 也是错误的
  3. # 正确的(正如Nico Van Belle所提到的)是:
  4. spring.jpa.hibernate.ddl-auto=create-drop

如果你运行一个真实的数据库(不是H2),默认的 ddl-auto= 是 none。

在我看来,导入自动配置类不是最好的方法,但它可以工作。

英文:

I think I found the error

  1. Spring applikation simple without imports
  1. @SpringBootApplication(scanBasePackages = &quot;eu.dwich.btc&quot;, exclude = ErrorMvcAutoConfiguration.class)
  2. public class Application extends SpringBootServletInitializer {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class, args);
  5. }
  6. }
  1. the backend config needs some modifikation
  1. @Configuration
  2. @EnableTransactionManagement
  3. @EntityScan(basePackages = &quot;eu.dwich.btc&quot;)
  4. @EnableJpaRepositories(basePackages = &quot;eu.dwich.btc&quot;)
  5. @Import({
  6. DataSourceAutoConfiguration.class,
  7. HibernateJpaAutoConfiguration.class,
  8. DataSourceTransactionManagerAutoConfiguration.class })
  9. @Profile(&quot;!test&quot;)
  10. public class BtcBackendLoginConfig {
  11. }

the second @SpringBootApplication was an error and is not needet
without the Autoconfig imports it won't work

and be carefull with the properties, tada.....

  1. #spring.jpa.properties.hibernate.ddl-auto=create-drop is wrong
  2. #spring.jpa.hibernate.hbm2ddl.auto=create-drop is also wrong
  3. # the right one (as mentioned by Nico Van Belle) is:
  4. spring.jpa.hibernate.ddl-auto=create-drop

and if your run an real database (not H2) the default ddl-auto= is none

In my opinion the import of the autoconfig classes is not the best way, but it works.

huangapple
  • 本文由 发表于 2020年9月21日 22:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定