[英]object references an unsaved transient instance - save the transient instance before flushing : Spring Data 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.