当前位置: 首页 > 知识库问答 >
问题:

如何在Spring Boot项目中的应用程序启动时创建示例实体?

巫马修然
2023-03-14

我想知道如何在启动时创建示例实体,以便在Spring Boot应用程序中测试我的前端。现在,我试图使用一个命令LineRunner,但我得到了这个错误:

    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

.... .... ....

这个错误有什么解决办法吗?或者用其他方法在数据库中创建样本(测试)实体?

共有1个答案

缪茂勋
2023-03-14

就错误而言,这可能是因为实体上的级联关系设置不正确。有关此问题的帮助,请参阅下面的文章:

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

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

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

 类似资料:
  • 我想创建Spring云项目,其中父项目是我的常见项目,所有其他模块都依赖于这个自定义的父项目,而不是作为父项目的“org.springframework.cloud”。 我不想在我的父母pom中添加这个 简单地说,我不想在maven模块层次结构中添加spring云作为父级。 如果使用spring初始值设定项spring初始值设定项生成项目,则 http://maven.apache.org/xsd

  • 我正在研究NFC应用程序。当我打开应用程序并从NFC标签读取信息时,活动打开哪个注册了意图过滤器,这将打开一个已经打开的应用程序的新实例。如何关闭应用程序的上一个实例或打开应用程序的上一个实例。 请帮帮我,抱歉英语不好。。提前谢谢。

  • 问题内容: 我正在创建一个Python脚本,其中执行了许多任务,其中一个任务是启动和打开Excel实例。在我的脚本中完成该任务的理想方法是什么? 问题答案: 尽管对于一般情况来说答案是合理的,但是如果您想对它做一些有用的事情,我建议您针对这种情况: 它是这样的: 取自邮件列表帖子,但周围有很多示例。

  • 当我尝试运行spring boot应用程序时,我遇到了以下异常: 组织。springframework。豆。工厂BeanCreationException:创建名为“configurationPropertiesBeans”的bean时出错,该bean在类路径资源[org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebi

  • 我需要在React中使用CORS节点模块,React是使用实用程序创建的。 由于它是一个实用程序,我不能调整内部并将CORS注入预配置的EXPRESS模块。 我们如何才能做到这一点?

  • 问题内容: 我正在创建一个Python脚本,它在其中执行一系列任务 任务是启动并打开一个Excel实例。最理想的方法是什么 在我的剧本里完成了吗? 问题答案: 虽然“Popen”的答案对一般情况来说是合理的,但我想 如果您想做些什么,请为这种特定情况推荐“win32api” 使用它很有用: 它是这样的: 取自[邮件列表](https://mail.python.org/pipermail/pyth