I was wondering how can I create sample entities on startup to test my front end with in a Spring Boot application. Right now I'm trying to use a CommandLineRunner but I get this error:
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
.... .... ....
Any solution to this error? Or maybe any other way to create sample (test) entities in the database?
CodePudding user response:
In regards, to the error, it is possibly because the cascade relationships on your entities are not set properly. Do refer to the article below for help on the issue:
https://www.baeldung.com/hibernate-unsaved-transient-instance-error
There are multiple ways to load initial data into your application. Using the command-line runner is a valid approach. For more alternative ways refer to the article below:
https://www.baeldung.com/spring-boot-data-sql-and-schema-sql
