java - 我正在尝试将我的应用程序从 Spring 转移到 Spring Boot,但遇到了一些问题。特别是以下异常

标签 java spring hibernate spring-boot exception

我根本没有使用实体管理器。相反,我使用 Hibernate( session 工厂)。这是我的应用程序属性

mysql.driver=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring_hiber?verifyServerCertificate=false&useSSL=false&requireSSL=false&useLegacyDatetimeCode=false&amp&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password

# Hibernate properties
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create



Exception in thread "task-2" java.lang.IllegalStateException: EntityManagerFactory is closed
at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:507)
at org.hibernate.internal.SessionFactoryImpl.getProperties(SessionFactoryImpl.java:501)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.findDataSource(DataSourceInitializedPublisher.java:105)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:97)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:830)

最佳答案

Spring Boot 会自动配置 Spring Data 相关的 Bean,无论你是否使用它。 如果您不想使用EntityManager,则排除这些与关系数据库相关的自动配置类。

在 Spring Boot 应用程序中你需要做

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@SpringBootApplication
public class SpringApplication {
  //...
}

关于java - 我正在尝试将我的应用程序从 Spring 转移到 Spring Boot,但遇到了一些问题。特别是以下异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61906492/

相关文章:

java - 将文件中的整数读入数组列表

java - 在 Hibernate Search 中使用多个关键字进行索引搜索

java - 使用没有 ID 的实体

spring - 如何在 Springs MVC 4 中使用 HikariCP 和 JdbcTemplate

java - 在 Java 中的数据库之间切换

java - Clojure JUNG setVertexFillPaintTransformer

spring - 事务服务中的 ConstraintViolationException 没有回滚

spring - 我可以在 tomcat 上使用一个 hibernate 持久性单元并在部署的 war 之间共享它吗?

java - Spring Data JPA - 实体名称无效标识符

java - JPA在多个事务中插入行,限制单个事务中的最大行数