-->
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.  [ 1 post ] 
Author Message
 Post subject: 2 opens sessions with GenericDao and proxy
PostPosted: Thu Jun 09, 2011 7:58 am 
Newbie

Joined: Tue May 31, 2011 6:41 am
Posts: 8
Hi everybody.
I have a problem witth Illegal attempt to associate a collection with two open sessions
Someone can help me please ?

This is my genericDao
Code:
public interface GenericDao<T, PK extends Serializable> {   
   PK save(T newInstance);
   T findById(PK id);
   List<T> findAll();
    void update(T transientObject);
    void remove(T persistentObject);
    void saveOrUpdate(T object);
}


and my genericdaoImpl
Code:
public class GenericDaoImpl<T, PK extends Serializable> implements GenericDao<T, PK> {
   @Autowired
   private SessionFactory sessionFactory;

    public GenericDaoImpl() {
      super();
   }

   private Class<T> type;

   public void saveOrUpdate(T o) {
       try {
          getSession().saveOrUpdate(o);
       } catch (HibernateException e) {
               e.printStackTrace();
           }       
   }
    public GenericDaoImpl(Class<T> type) {
        this.type = type;
    }

   public PK save(T o) {
       return (PK) getSession().save(o);
    }

   public T findById(PK id) {
        return (T) getSession().get(type, id);
    }

   public List<T> findAll() {
        Criteria crit = getSession().createCriteria(type);
        crit.add(Restrictions.eq("enabled", new Boolean(true)));
        return crit.list();
    }

    public void update(T o) {
          getSession().update(o);
    }
   
    public void merge(T o) {
      getSession().merge(o);
    }

   public void remove(T o) {
        getSession().delete(o);
    }

    public Session getSession() {
        boolean allowCreate = true;
        return SessionFactoryUtils.getSession(sessionFactory, allowCreate);
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}


and my class impl
Code:
@Transactional
@Service("SfdService")
public class SfdServiceImpl extends GenericDaoImpl<Sfd, Integer>implements SfdService,Serializable{
   public SfdServiceImpl(Class<Sfd> type) {
      super(type);
   }
   public SfdServiceImpl() {
      super();
   }
}


Wierdly If I do this:
Sfd sfd=sfdService.findById(1);
sfd.setNom("changeName");
sfdService.save(sfd);

I got an error "org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions"

i suspect my getsession because i think that he create a new session everytime.
Someone can help me please ???


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

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.