简体   繁体   English

保存实体时JPA错误,对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例

[英]JPA error while saving entity, object references an unsaved transient instance - save the transient instance before flushing

I get following error, 我收到以下错误,

object references an unsaved transient instance - save the transient instance before flushing: Nominee; 对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例: nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Nominee object references an unsaved transient instance - save the transient instance before flushing 嵌套异常是java.lang.IllegalStateException:org.hibernate.TransientObjectException:对象引用了一个未保存的临时实例-在刷新之前保存了该临时实例:被提名对象引用了一个未保存的临时实例-在刷新之前保存了该临时实例。

at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381) at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:227) at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springf 在org.org.org上的org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialm.Jb:227work)在org.spring.org.org.org.springframework.orm.jpa。 org.org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)的org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)的.doCommit(JpaTransactionManager.java:521)。在org.springframework.transaction.interceptor.Transaction。 java:96)在org.springf ramework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) at com.sun.proxy.$Proxy180.save(Unknown Source) org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)上的ramework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation。 java:179)(位于org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131)处,org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)的.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)在org.springframework.aop.framework.JdkDynamicAopProxy.in .java:208),位于com.sun.proxy。$ Proxy180.save(未知来源)

I have 2 entity class Employee.java (t_employee) and Nominee.java(t_nominee) where an employee can have many numbers of nominee so I created an association table called ta_empl_nom (I wanted as association table itself, because later I may have option linking other employees to existing nominee) 我有2个实体类Employee.java(t_employee)和Nominee.java(t_nominee),其中一个雇员可以有很多被提名人,所以我创建了一个名为ta_empl_nom的关联表(我想作为关联表本身,因为以后我可能会有选项链接现有雇员的其他雇员)

so here when I fetch employee object, I want map of nominee object with key as nominee name and object nominee itself. 因此,在这里,当我获取员工对象时,我希望使用键作为被提名者名称和被提名者本身的被提名对象的映射。 I succeeded in getting the object. 我成功地获得了对象。

but problem while saving. 但是在保存时出现问题。 when I save employee object it should save its nominee details also. 当我保存员工对象时,还应该保存其被提名人的详细信息。

Here is the entity classes Employee.java 这是实体类Employee.java

@Entity
@Table( name = "t_employee" )
public class Employee {
    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private long id;
    @Column( name = "name" )
    private String name;

@ElementCollection( fetch = FetchType.LAZY )
    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable( name = "ta_emp_nom", joinColumns = @JoinColumn( name = "employee" ), inverseJoinColumns = @JoinColumn( name = "nominee" ) )
    @MapKey( name = "name" )
    private Map<String, Nominee> nomineeMap;

//getters and setters

}

Here is the Nominee entity class Nominee.java 这是Nominee实体类Nominee.java

@Entity
@Table( name = "t_nominee" )
public class Nominee {

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private long id;

    @Column( name = "name" )
    private String name;

// other fields, getters, and setter for them below
}

Here is my service layer 这是我的服务层

Employee emp = new Employee();
emp.setName("Rahul");
Map<String,Nominee> nomineeMap = new HashMap<>();
Nominee nom1 = new Nominee();
nom1.setName("nom1");
Nominee nom2 = new Nominee();
nom1.setName("nom2");
nomineeMap.put(nom1.getName(), nom1);
nomineeMap.put(nom2.getName(), nom2);
emp.setNominee(nomineeMap);
employeeRepository.save(emp); //error here while saving this emp obj

I am getting above error message while saving. 保存时出现上述错误消息。

You may be updating a record when you should be creating it in the persistence, because it is new. 您应该在持久性中创建记录时更新记录,因为它是新记录。 So, use your Entity Manager to do persist in the object and any foreign key it has. 因此,请使用您的实体管理器来持久化对象及其所具有的任何外键。

Your answer is already provided in the first line of the exception. 例外情况的第一行中已经提供了您的答案。 You probably just saved the object in session but didn't commit the traction. 您可能只是在会话中保存了对象,但没有提交牵引力。 All other parts are correct just commit the transaction. 其他所有部分都是正确的,只需提交事务即可。 Make sure that you have mapped both Entity classes in hibernate configuration file and all configuration is done properly. 确保已在休眠配置文件中映射了两个实体类,并且所有配置均正确完成。 Now your persistence method should look like this. 现在,您的持久性方法应如下所示。 [I have created a SessionFactory object here but you should create only one object for a database in the whole project.] [我在这里创建了一个SessionFactory对象,但是您应该在整个项目中为数据库创建一个对象。]

    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = null;
    Transaction transaction = null;
    try {

        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        Employee emp = new Employee();
        emp.setName("Rahul");
        Map<String, Nominee> nomineeMap = new HashMap<>();
        Nominee nom1 = new Nominee();
        nom1.setName("nom1");
        Nominee nom2 = new Nominee();
        nom1.setName("nom2");
        nomineeMap.put(nom1.getName(), nom1);
        nomineeMap.put(nom2.getName(), nom2);
        emp.setNomineeMap(nomineeMap);
        session.save(emp);
        transaction.commit();
    } catch (Exception e) {

        if (transaction != null) {
            transaction.rollback();
        }
    } finally {

        session.close();
        sessionFactory.close();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例:Spring Data JPA - object references an unsaved transient instance - save the transient instance before flushing : Spring Data JPA JPA对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例 - JPA object references an unsaved transient instance - save the transient instance before flushing 对象引用了未保存的瞬态实例-在刷新休眠JPA之前保存瞬态实例 - Object references an unsaved transient instance - save the transient instance before flushing hibernate JPA Spring Data/JPA 和 errorType :对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - Spring Data/JPA and errorType : object references an unsaved transient instance - save the transient instance before flushing 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.entity.Role - object references an unsaved transient instance - save the transient instance before flushing: com.entity.Role 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing? Object 引用了未保存的瞬态实例 在刷新错误之前保存瞬态实例 - Object references an unsaved transient instance save the transient instance before flushing error 如何修复 Hibernate“对象引用未保存的瞬态实例 - 在刷新前保存瞬态实例”错误 - How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing TransientObjectException:对象引用了一个未保存的临时实例-在执行合并时在刷新之前保存该临时实例 - TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing when I am doing merge
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM