title: Hibernate Error Message: Save the transient instance before flushing
body: <p>While trying to create a one to many relationship I am geeting &quot;Save the transient instance before flushing&quot; error message usign Spring JPA connection to PostgreSQL.
  &quot;Alquiler&quot;(One) is the name of my entity and the other is &quot;Peliculas&quot;(To Many)</p>
  <p>Notice that I already added cascade = CascadeType.ALL And still getting the issue.
  This is the class where I am trying to create the relationship. Help!</p>
  <pre><code>package com.Project.Movies.model;
  
  import java.util.Date;
  import java.util.List;
  
  import javax.persistence.*;
  
  @Entity
  @Table(name = &quot;Alquiler&quot;)
  public class Alquiler {
  @Id
  @SequenceGenerator(
          name = &quot;alquiler_seq&quot;,
          sequenceName = &quot;alquiler_seq&quot;,
          allocationSize = 1
          )
  @GeneratedValue(
          strategy = GenerationType.SEQUENCE,
          generator = &quot;alquiler_seq&quot;
          )
  
  private Long id;
  
  int dias;
  float costopordia;
  String formapago;
  Date fechaAlquiler;
  
  @Column
  @ElementCollection(targetClass=Pelicula.class)
  private List&lt;Pelicula&gt; peliculas;
  
  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = &quot;id&quot;)
  private Socio socio;
  
  
  public Alquiler() {
      super();
  }
  
  public Alquiler(int dias, float costopordia, String formapago, Date fechaAlquiler, List&lt;Pelicula&gt; pelicula, Socio socio) {
      super();
      this.dias = dias;
      this.costopordia = costopordia;
      this.formapago = formapago;
      this.fechaAlquiler = fechaAlquiler;
      this.peliculas = pelicula;
      this.socio = socio;
  }
  
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public Date getFechaAlquiler() {
      return fechaAlquiler;
  }
  
  public void setFechaAlquiler(Date fechaAlquiler) {
      this.fechaAlquiler = fechaAlquiler;
  }
  
  @OneToMany(targetEntity=Pelicula.class, cascade = CascadeType.ALL,orphanRemoval = true)
  public List&lt;Pelicula&gt; getPeliculas() {
      return peliculas;
  }
  
  public void setPeliculas(List&lt;Pelicula&gt; pelicula) {
      this.peliculas = pelicula;
  }
  
  public Socio getSocio() {
      return socio;
  }
  
  public void setSocio(Socio socio) {
      this.socio = socio;
  }
  
  boolean CrearAlquiler() {
      return true;
  }
  
  public int getDias() {
      return dias;
  }
  
  public void setDias(int dias) {
      this.dias = dias;
  }
  
  public float getCostopordia() {
      return costopordia;
  }
  
  public void setCostopordia(float costopordia) {
      this.costopordia = costopordia;
  }
  
  public String getFormapago() {
      return formapago;
  }
  
  public void setFormapago(String formapago) {
      this.formapago = formapago;
  }
  </code></pre>
  <p>}</p>
  <p>Alquiler Config Class</p>
  <pre><code>package com.Project.Movies.model;
  
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  public class AlquilerConfig {
  @Bean
  CommandLineRunner commandLineRunnerA(IAlquilerRepository ialquilerRepository)
  {
      
  return args -&gt;{
      
      List&lt;Pelicula&gt; myList = new ArrayList&lt;Pelicula&gt;();
      
      Pelicula shawshank = new Pelicula(&quot;The Shawshank Redemption&quot;,1994,&quot;2hr 22mins&quot;,&quot;Two imprisoned men bond over a number of years, finding solace and eventual redemption through...&quot;,&quot;Frank Darabont&quot;,&quot;B&quot;,&quot;Drama&quot;,3);
      Pelicula inception = new Pelicula(&quot;Inception&quot;,2010,&quot;2hrs 28mins&quot;,&quot;Dom Cobb es un ladrón con una extraña habilidad para entrar a los sueños de la gente y robarles los secretos de sus subconscientes&quot;,&quot;Christopher Nolan&quot;,&quot;C&quot;,&quot;Science Fiction&quot;,4);
      Pelicula fightclub = new Pelicula(&quot;Fight Club&quot;,1999,&quot;2hrs 19mins&quot;,&quot;Un empleado de oficina insomne, harto de su vida, se cruza con un vendedor peculiar...&quot;,&quot;David Fincher&quot;,&quot;C&quot;,&quot;Suspenso/Drama&quot;,2);                 
      
      myList.add(inception);
      
      myList.add(shawshank);
      myList.add(inception);
      myList.add(fightclub);
      
      Socio mariana= new Socio(&quot;Mariana Navidad&quot;,&quot;Arcos #34, Los Arcos&quot;,&quot;4446784563&quot;);
      Socio sebastian= new Socio(&quot;Sebastian Mendoza&quot;,&quot;Rios #38, Mares&quot;,&quot;4336784563&quot;);
      Socio roberto= new Socio(&quot;Roberto Obregon&quot;,&quot;Estrella #89, Universo&quot;,&quot;4556784563&quot;);
      
      Alquiler shawshankAlquiler = new Alquiler(3,30,&quot;efectivo&quot;,new Date(),myList,mariana);
      Alquiler inceptionAlquiler = new Alquiler(5,25,&quot;tarjeta&quot;,new Date(),myList,sebastian);
      Alquiler inceptionAlquiler2 = new Alquiler(5,25,&quot;efectivo&quot;,new Date(),myList,roberto);
      
      ialquilerRepository.saveAll(List.of(shawshankAlquiler,inceptionAlquiler,inceptionAlquiler2));
  };  
  }
  </code></pre>
  <p>}</p>
  <p>Error Message</p>
  <p>java.lang.IllegalStateException: Failed to execute CommandLineRunner
  at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:794) ~[spring-boot-2.5.6.jar:2.5.6]
  at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:775) ~[spring-boot-2.5.6.jar:2.5.6]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:345) ~[spring-boot-2.5.6.jar:2.5.6]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.6.jar:2.5.6]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.6.jar:2.5.6]
  at com.univaProject.UnivaMovies.UnivaMoviesApplication.main(UnivaMoviesApplication.java:11) ~[classes/:na]
  Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.univaProject.UnivaMovies.model.Pelicula; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.univaProject.UnivaMovies.model.Pelicula
  at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371) ~[spring-orm-5.3.12.jar:5.3.12]
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235) ~[spring-orm-5.3.12.jar:5.3.12]
  at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:566) ~[spring-orm-5.3.12.jar:5.3.12]
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:743) ~[spring-tx-5.3.12.jar:5.3.12]
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711) ~[spring-tx-5.3.12.jar:5.3.12]
  at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:654) ~[spring-tx-5.3.12.jar:5.3.12]
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:407) ~[spring-tx-5.3.12.jar:5.3.12]
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.12.jar:5.3.12]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.12.jar:5.3.12]
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.12.jar:5.3.12]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.12.jar:5.3.12]
  at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) ~[spring-data-jpa-2.5.6.jar:2.5.6]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.12.jar:5.3.12]
  at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.12.jar:5.3.12]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.12.jar:5.3.12]
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.12.jar:5.3.12]
  at com.sun.proxy.$Proxy87.saveAll(Unknown Source) ~[na:na]
  at com.univaProject.UnivaMovies.model.AlquilerConfig.lambda$0(AlquilerConfig.java:39) ~[classes/:na]
  at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) ~[spring-boot-2.5.6.jar:2.5.6]
  ... 5 common frames omitted</p>