Hibernate org.hibernate.TransientObjectException: object references an unsaved transient instance

62 Views Asked by At

I searched above error message and found several questions related to, like Object references an unsaved transient instance save the transient instance before flushing error and object references an unsaved transient instance - Spring, JPA Hibernate and How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error but they don't seem like mine.

My project is Spring boot, using hibernate to interact with Oracle database.

My entity:

@Entity
@Table(name = "student")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    String name;
    String address;
    LocalDateTime createdAt;
    LocalDateTime updatedAt;
    // Getter
    // Setter
}

I have a procedure which takes a List of Student as IN parameter, and List of Student as OUT parameter, so I declare it as an IN OUT parameter. It works like below:

// Loop the input list 
// Each student only has name and address, id & createdAt & updatedAt are generated automatically by Oralcle
// Returning id & createdAt & updatedAt to each student
// Return the list

I created a SQL script to call above procedure and it works perfectly.

This is how I call the procedure from my application:

public interface StudentRepository extends JpaRepository<Student, Long> {

    @Query(value = "execute my_procedure(:covenants)", nativeQuery = true)
    List<Student> saveBulk(@Param("students") List<Student> students);
}

public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public void saveBulk(List<Student> students){
        studentRepository.saveBulk(students);
    }
}

An exception ocurred:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.myapplication.entity.Student; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.myapplication.entity.Student
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:241)
    at com.sun.proxy.$Proxy245.saveBulk(Unknown Source)

I tried to change from @Query to @Procedure and added NamedStoredProcedureQuery to Student but it does not work also.

How can I call this procedure ? Thank you in advance

1

There are 1 best solutions below

1
Aakash On

I don't see @Id annotation in your entity. This could be the problem.