这是错误:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.project.lmu.data.entity.Company#02K]
org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:638)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:112)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
org.hibernate.engine.Cascade.cascade(Cascade.java:161)
org.hibernate.engine.Cascade.cascade(Cascade.java:127)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:376)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:350)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:112)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
org.hibernate.engine.Cascade.cascade(Cascade.java:161)
org.hibernate.engine.Cascade.cascade(Cascade.java:127)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:376)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:350)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:112)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
com.project.lmu.data.dao.hibernate.AbstractHibernateDAO.saveOrUpdate(AbstractHibernateDAO.java:172)
com.project.lmu.data.tx.AbstractTxService.update(AbstractTxService.java:68)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy48.update(Unknown Source)
com.project.lmu.data.service.impl.CoworkerServiceImpl.genUpdateNomination(CoworkerServiceImpl.java:175)
com.project.lmu.controller.coworkerNom.CoworkerController.genUpdateNomination(CoworkerController.java:305)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
现在这是失败的方法:
public void genUpdateNomination(int nominationId, int statusId, String generalistComment, int generalistId) {
CoworkerNom nom = coworkerNomTxService.findById(nominationId);
Status status = statusTxService.findById(statusId);
if (!(nom.getStatus() == status && nom.getGeneralistComment().equals(generalistComment))) {
if (statusId == 2) {
//Approved so do email
}
if (statusId == 3) {
//Does Not Proceed so do email
}
if (statusId == 4) {
//Incomplete so do email
}
Employee generalist = employeeTxService.findById(generalistId);
nom.setGeneralist(generalist);
nom.setGeneralistComment(generalistComment);
nom.setStatus(status);
nom.setUpdatedBy(generalist);
coworkerNomTxService.update(nom);
}
}
现在我将其归结为实体Employee
和Nomination
的{{1}}和Nominator
(也是员工对象)。这是Nominee
:
Nomination
这是@Entity(name = "Nomination")
@Table(name = "NOMINATION")
@DiscriminatorColumn(name="CATEGORY_CODE", discriminatorType = DiscriminatorType.STRING, length = 1)
@Inheritance(strategy=InheritanceType.JOINED)
public /*abstract*/ class Nomination extends AuditableEntity {
@Id
@Column(name = "NOM_ID", insertable = true, updatable = true,
nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "NOMINATOR_ID", referencedColumnName = "EMP_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE})
private Employee nominator = null;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "GENERALIST_ID", referencedColumnName = "EMP_ID", nullable = true)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE})
private Employee generalist = null;
:
Employee
现在这些对象在我的应用程序中使用了TON方法。事实上,我刚刚在上一个方法/添加中遇到了这个错误,以便进入测试/测试用例模式。
如果我必须粘贴更多方法,请告诉我。我不知道是什么原因引起的。这里唯一的新功能是设置@Entity(name = "Employee")
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
@Id
@Column(name = "EMP_ID", insertable = true, updatable = false, nullable = false)
private int id;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "COMPANY_CODE", referencedColumnName = "COMPANY_CODE")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE})
private Company company = null;
。
答案 0 :(得分:1)
我遇到过同样的错误一次。它出现在多个到一个或多个关系中,主要原因是一个对象在一个会话中打开,但它在另一个会话中被引用到另一个对象。我希望这reference有所帮助。
答案 1 :(得分:1)
我在尝试了很多事情之后解决了这个问题,但最终工作的是取代它:
coworkerNomTxService.update(nom);
merge()
如下:
coworkerNomTxService.merge(nom);
我发布在这里,所以希望如果你得到同样的错误,你将首先尝试这种方法。如果不是,请尝试检查CascadeType是否设置为ALL和。
答案 2 :(得分:0)
我经常做的第一件事是在session.getStatistics().getEntityKeys()
方法调用之前调用session.update()
并查看是否有任何重复的对象。如果是,请使用相同的方法追踪它。
其次,确保更新的对象'儿童集合(如果有的话)没有相同的ID。
希望有所帮助