Hibernate org.hibernate.TransientObjectException:对象引用未保存的瞬态实例

问题描述 投票:0回答:1

我搜索了上面的错误消息,发现了几个相关的问题,例如对象引用未保存的瞬态实例在刷新错误之前保存瞬态实例对象引用未保存的瞬态实例 - Spring,JPA Hibernate如何修复Hibernate “对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例”错误但它们看起来不像我的。

我的项目是Spring boot,使用hibernate与Oracle数据库交互。

我的实体:

@Entity
@Table(name = 'student')
public class Student {
    Long id;
    String name;
    String address;
    LocalDateTime createdAt;
    LocalDateTime updatedAt;
    // Getter
    // Setter
}

我有一个程序,将

List
Student
作为 IN 参数,将
List
Student
作为 OUT 参数,其工作原理如下:

// 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

我创建了一个 SQL 脚本来调用上面的过程,它运行得很好。

这是我从应用程序中调用该过程的方式:

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);
    }
}

发生异常:

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)

我尝试从

@Query
更改为
@Procedure
并将
NamedStoredProcedureQuery
添加到
Student
但它也不起作用。

如何调用这个程序?先谢谢你了

java oracle hibernate procedure
1个回答
0
投票

我在您的实体中没有看到

@Id
注释。这可能就是问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.