0

这是错误:

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

现在我把它归结为 entityEmployeeNomination's Nominatorand Nominee(它们也是 Employee 对象)。这里是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

@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;

现在这些对象已经在我的应用程序中使用了大量的方法。事实上,为了进入测试/测试用例模式,我刚刚在最后一个方法/添加中遇到了这个错误。

如果我必须粘贴更多方法,请告诉我。我不知道是什么原因造成的。这里唯一的新东西是设置generalist.

4

3 回答 3

1

我在尝试了很多事情后解决了这个问题,但最终的工作是替换这个:

       coworkerNomTxService.update(nom);

merge()这样:

       coworkerNomTxService.merge(nom);

我在这里发帖,所以希望如果你遇到同样的错误,你会先尝试这种方法。如果不尝试检查 CascadeType 是否设置为 ALL 和。

于 2012-04-18T14:03:10.590 回答
1

我曾经遇到过同样的错误。它以多对一或多对多关系出现,主要原因是一个对象在一个会话中打开,但它在其他会话中被引用到另一个对象。我希望这个参考有帮助。

于 2012-04-17T20:06:24.557 回答
0

我总是做的第一件事是在方法调用session.getStatistics().getEntityKeys()之前session.update()调用,看看是否有任何重复的对象。如果是,请使用相同的方法进行追踪。

其次,确保您更新的对象的子集合(如果有)没有相同的 id。

希望能帮助到你

于 2016-06-30T03:04:12.200 回答