【问题标题】:How to create sample entities on app startup in a Spring Boot project?如何在 Spring Boot 项目中的应用启动时创建示例实体?
【发布时间】:2022-01-24 15:52:42
【问题描述】:

我想知道如何在启动时创建示例实体以在 Spring Boot 应用程序中测试我的前端。现在我正在尝试使用 CommandLineRunner 但我收到此错误:

    java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) ~[spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:344) ~[spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1336) ~[spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1325) ~[spring-boot-2.5.0.jar:2.5.0]
    at it.uniroma3.siw.SiwSpringSecurityApplication.main(SiwSpringSecurityApplication.java:24) ~[classes/:na]
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : it.uniroma3.siw.spring.model.Intervento.meccanico -> it.uniroma3.siw.spring.model.Meccanico; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : it.uniroma3.siw.spring.model.Intervento.meccanico -> it.uniroma3.siw.spring.model.Meccanico

.... …… ....

这个错误有什么解决办法吗?或者可能有任何其他方式在数据库中创建示例(测试)实体?

【问题讨论】:

    标签: java mysql spring database security


    【解决方案1】:

    关于该错误,可能是因为您的实体上的级联关系设置不正确。请参阅以下文章以获取有关此问题的帮助:

    https://www.baeldung.com/hibernate-unsaved-transient-instance-error

    有多种方法可以将初始数据加载到您的应用程序中。使用命令行运行器是一种有效的方法。更多替代方式请参考以下文章:

    https://www.baeldung.com/spring-boot-data-sql-and-schema-sql

    【讨论】:

      猜你喜欢
      • 2019-10-04
      • 2017-06-14
      • 2019-05-29
      • 2020-08-04
      • 2016-02-17
      • 2020-07-23
      • 1970-01-01
      • 2019-06-20
      • 2016-06-01
      相关资源
      最近更新 更多