-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: NonUniqueObjectException: when updating an entity
PostPosted: Tue Oct 25, 2011 3:38 pm 
Newbie

Joined: Mon Oct 17, 2011 1:50 pm
Posts: 5
i have three entities

-Authority , Permission , PermissionCategory

-Desc: authority can contain many permissions, and permission can be in many authorities, and permissionCategory can contain many permissions.

1- Authority > Many To Many With Permission

Code:
class Authority{
       
        @ManyToMany(cascade = CascadeType.ALL)
           @JoinTable(name = "authority_permission", joinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "permission_id", nullable = false, updatable = false) })
           private Set<Permission> permissions = new HashSet<Permission>(0);
       
        }


2- PermissionCategory >
One To Many With Permission



Code:
class PermissionCategory{
   
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
           @JoinTable(name = "perm_cat_map", joinColumns = { @JoinColumn(name = "perm_cat_id") }, inverseJoinColumns = { @JoinColumn(name = "permission_id") })
           private Set<Permission> permissions = new HashSet<Permission>(0);
       
           }


3- Permission >
Many To One With PermissionCategory

Code:
    class Permission{
   
    @ManyToOne(cascade = CascadeType.ALL)
    private PermissionCategory permissionCategory;
   
    }


Case 1: Adding a new role

- When adding a new role, user chooses already existing permissions, so i need to insert in authority and in join table (Authority_Permission)

so i make it as follows:
Code:
    Set<Permission> permissions=new HashSet<Permission>;
    Permission permission=permissionDao.getPermissionById(1);
    permissions.add(permission);   
    authority.setPermissions(permissions);
    sessionFactory.getCurrentSession().merge(authority); // it works only with merge


this case works fine.

Case 2: Updating a role

- User may change the role properties, and the associated permissions, so what's needed is to updated the authority object and update the associated permissions, if a permission was removed then remove it and if a permission was added to add it.

so i made it as follows:

Code:
   
        Set<Permission> permissions=new HashSet<Permission>;
        Permission permission=permissionDao.getPermissionById(1);
        permissions.add(permission);   
        authority.setPermissions(permissions);
        sessionFactory.getCurrentSession().update(authority);


problem is when trying to update a role as mentioned above, i am getting exception:


Code:
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]
    javax.faces.FacesException: #{authority.addOrUpdateAuthority}: org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]
       at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
       at javax.faces.component.UICommand.broadcast(UICommand.java:315)
       at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
       at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
       at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
       at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
       at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
       at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:99)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:57)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
       at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
       at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
       at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
       at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
       at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:110)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
       at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
       at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
       at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
       at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
       at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
       at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
       at java.lang.Thread.run(Thread.java:619)
    Caused by: javax.faces.el.EvaluationException: org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]
       at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
       at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
       ... 87 more
    Caused by: org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]
       at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
       at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
       at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
       at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
       at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
       at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
       at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
       at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
       at $Proxy30.updateAuthority(Unknown Source)
       at com.myapp.beans.AuthorityBean.addOrUpdateAuthority(AuthorityBean.java:110)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
       at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
       at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
       at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
       ... 88 more
    Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.myapp.domain.Permission#3]
       at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:637)
       at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305)
       at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
       at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:112)
       at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
       at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677)
       at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669)
       at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
       at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
       at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425)
       at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362)
       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338)
       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
       at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
       at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154)
       at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:145)
       at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
       at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
       at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
       ... 104 more


please advise.


Top
 Profile  
 
 Post subject: Re: NonUniqueObjectException: when updating an entity
PostPosted: Wed Oct 26, 2011 5:54 am 
Newbie

Joined: Mon Oct 17, 2011 1:50 pm
Posts: 5
well it works fine after i make merging then save or update, as follows:

1- Save:

Code:
authority = (Authority) getCurrentSession().merge(authority);
    sessionFactory.getCurrentSession().save(authority);



2- Update:

Code:
authority = (Authority) getCurrentSession().merge(authority);
    sessionFactory.getCurrentSession().update(authority);


but i need some experienced explanation here from anyone, why i need merging then save or update, and if that solution is the best, just found it in a blog.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.