-->
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.  [ 5 posts ] 
Author Message
 Post subject: Illegal attempt to associate a collection with two open sess
PostPosted: Tue Mar 15, 2016 4:14 pm 
Newbie

Joined: Tue Mar 15, 2016 4:10 pm
Posts: 6
Hello guys

I'm losing several nights with this problem
There are are many questions of same type, but none works for me.
someone could help me, please?

I have JSF Hibernate application

TeamModel

Code:
@Entity
@Table(name="team")
public class Team implements Serializable{
   
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id_team",nullable=false)
    private Integer idTeam;
   
    @Column(name="team",nullable=false)
    private String team;
   
    @OneToMany(fetch=FetchType.LAZY, targetEntity=Usuario.class,mappedBy="team")
    private Set usuarios = new HashSet(0);
   
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="id_flm")
    private Flm flm;


---------------------

TeamDaoImpl

Code:
@Override
    public void atualizar(Team team) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            session.update(team);
            session.beginTransaction().commit();
        } catch (Exception e){
            System.out.println("Erro ao inserir: "+e.getMessage());
            session.beginTransaction().rollback();
        } //finally {
            //session.close();
        //}
   }

@Override
    public Team findById(Integer id_team) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Team) session.load(Team.class, id_team);
    }

@Override
    public List<Team> findAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from Team ORDER BY id_flm ASC").list();
    }

---------------------

TeamBean

Code:
@ManagedBean(name="teamBean")
@ViewScoped
public class TeamBean implements Serializable {


---------------

FlmDaoImpl

Code:
public class FlmDaoImpl implements FlmDao {

@Override
    public List<Flm> selectItems() {
         List<Flm> listado = null;
        Session sesion = HibernateUtil.getSessionFactory().getCurrentSession();
        String sql = "FROM Flm";
        try {
            sesion.beginTransaction();
            listado =  sesion.createQuery(sql).list();
            sesion.beginTransaction().commit();
        } catch (Exception e) {
            sesion.beginTransaction().rollback();
            System.out.println("Erro no selecItemsFLM: "+e.getMessage());
        }
        return listado;
    }


Note: merge() works only when update "team name" field, does not work to "flm" field

Image


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Wed Mar 16, 2016 1:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I don't see any merge() call and I could not understand what is not working.


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Wed Mar 16, 2016 11:47 pm 
Newbie

Joined: Tue Mar 15, 2016 4:10 pm
Posts: 6
I was using merge() as another alternative, but is not working fine...

Well.. I got the stracktrace now:

Code:
Grave: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
   at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
   at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:43)
   at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
   at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
   at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
   at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:293)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
   at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:564)
   at org.hibernate.impl.SessionImpl.update(SessionImpl.java:552)
   at org.hibernate.impl.SessionImpl.update(SessionImpl.java:544)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:606)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at com.sun.proxy.$Proxy143.update(Unknown Source)
   at com.changes.dao.impl.TeamDaoImpl.update(TeamDaoImpl.java:34)
   at com.changes.bean.TeamBean.update(TeamBean.java:68)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:606)
   at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779)
   at javax.el.BeanELResolver.invoke(BeanELResolver.java:528)
   at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257)
   at com.sun.el.parser.AstValue.invoke(AstValue.java:248)
   at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
   at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
   at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
   at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
   at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
   at javax.faces.component.UICommand.broadcast(UICommand.java:300)
   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.StandardWrapper.service(StandardWrapper.java:1550)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
   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:355)
   at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
   at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)
   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:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
   at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
   at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
   at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
   at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
   at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
   at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
   at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
   at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
   at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTas
Grave: k.java:54)
   at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
   at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
   at java.lang.Thread.run(Thread.java:745)


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Thu Mar 17, 2016 12:06 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
The exception originated at the update method of TeamBean and TeamDaoImpl, as per stacktrace below:

Quote:
at com.sun.proxy.$Proxy143.update(Unknown Source)
at com.changes.dao.impl.TeamDaoImpl.update(TeamDaoImpl.java:34)
at com.changes.bean.TeamBean.update(TeamBean.java:68)

Posting the update method of TeamDaoImpl and TeamBean would help.


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Thu Mar 17, 2016 10:46 am 
Newbie

Joined: Tue Mar 15, 2016 4:10 pm
Posts: 6
MadhusudanaSunnapu wrote:
The exception originated at the update method of TeamBean and TeamDaoImpl, as per stacktrace below:

Quote:
at com.sun.proxy.$Proxy143.update(Unknown Source)
at com.changes.dao.impl.TeamDaoImpl.update(TeamDaoImpl.java:34)
at com.changes.bean.TeamBean.update(TeamBean.java:68)

Posting the update method of TeamDaoImpl and TeamBean would help.


TeamDaoImpl.update is the start of the topic as TeamDaoImpl.atualizar (in portuguese update means atualizacao)
I will post the method again

Thanks

TeamDaoImpl
Code:
@Override
    public void atualizar(Team team) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.beginTransaction();
            session.update(team);
            session.getTransaction().commit();
        } catch (Exception e){
            System.out.println("Erro ao atualizar: "+e.getMessage());
            e.printStackTrace();
            session.getTransaction().rollback();
        } //finally {
            //session.close();
        //}
   }


TeamBean
Code:
public void atualizar(){
        try {
            TeamDao teamDao = new TeamDaoImpl();
            teamDao.atualizar(team);
            FacesContext context = FacesContext.getCurrentInstance();           
            context.addMessage(null, new FacesMessage("Team successfully updated."));
            team = new Team();
        } catch (Exception e) {
            System.out.println("Erro ao ATUALIZAR: "+e.getMessage());
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.