繁体   English   中英

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

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

我收到以下错误,

对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例: 嵌套异常是java.lang.IllegalStateException:org.hibernate.TransientObjectException:对象引用了一个未保存的临时实例-在刷新之前保存了该临时实例:被提名对象引用了一个未保存的临时实例-在刷新之前保存了该临时实例。

在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 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(未知来源)

我有2个实体类Employee.java(t_employee)和Nominee.java(t_nominee),其中一个雇员可以有很多被提名人,所以我创建了一个名为ta_empl_nom的关联表(我想作为关联表本身,因为以后我可能会有选项链接现有雇员的其他雇员)

因此,在这里,当我获取员工对象时,我希望使用键作为被提名者名称和被提名者本身的被提名对象的映射。 我成功地获得了对象。

但是在保存时出现问题。 当我保存员工对象时,还应该保存其被提名人的详细信息。

这是实体类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

}

这是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
}

这是我的服务层

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

保存时出现上述错误消息。

您应该在持久性中创建记录时更新记录,因为它是新记录。 因此,请使用您的实体管理器来持久化对象及其所具有的任何外键。

例外情况的第一行中已经提供了您的答案。 您可能只是在会话中保存了对象,但没有提交牵引力。 其他所有部分都是正确的,只需提交事务即可。 确保已在休眠配置文件中映射了两个实体类,并且所有配置均正确完成。 现在,您的持久性方法应如下所示。 [我在这里创建了一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM