Attempting to update with NHibernate?

1

I'm trying to make an update with NHibernate but it always returns me an error saying that there are two open sessions: illegally attempted to associate a proxy with two open session

How to solve this?

GenericDAO

public class GenericDAO<T> : IPersist<T> where T : class {

        public void insert(T obj) {
            ISession _session = DBConnect.openSession();
            ITransaction _transaction = _session.BeginTransaction();
            try {
                _session.Save(obj);
                _transaction.Commit();
            }catch (Exception e) {
                if (!_transaction.WasCommitted) {
                    _transaction.Rollback();
                }
                MessageBox.Show("Erro tentando salvar: " + e.Message, "Aviso",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        public void update(T obj) {
            ISession _session = DBConnect.openSession();
            ITransaction _transaction = _session.BeginTransaction();
            try {
                _session.Update(obj);
                _transaction.Commit();
            }catch (Exception e) {
                if (!_transaction.WasCommitted) {
                    _transaction.Rollback();
                }
                MessageBox.Show("Erro tentando alterar: " + e.Message, "Aviso", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        public void delete(T obj) {
            ISession _session = DBConnect.openSession();
            ITransaction _transaction = _session.BeginTransaction();
            try {
                _session.Delete(obj);
                _transaction.Commit();
            }catch (Exception e) {
                if (!_transaction.WasCommitted) {
                    _transaction.Rollback();
                }
                MessageBox.Show("Erro tentando deletar: " + e.Message, "Aviso", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        public T findObject(long id) {
            ISession _session = DBConnect.openSession();
            return _session.Load<T>(id);
        }


        public void saveOrUpdate(T obj) {
            ISession _session = DBConnect.openSession();
            ITransaction _transaction = _session.BeginTransaction();
            try {
                _session.SaveOrUpdate(obj);
                _transaction.Commit();
            }catch (Exception e) {
                if (!_transaction.WasCommitted) {
                    _transaction.Rollback();
                }
                MessageBox.Show("Erro tentando salvar ou alterar: " + e.Message, "Aviso",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

Update

/** insere Perfil + Modulo */
        private void insertPerfilModulo() {
            PermissaoDAO dao = new PermissaoDAO();
            Perfil perfil = (Perfil)cbxPerfilModulo.SelectedItem;
            IList<Modulo> lista = getListaModulo();

            foreach(Modulo m in lista){                
                Permissao permissao = new Permissao();
                permissao.perfil = perfil;
                permissao.modulo = m;

                Boolean exist = dao.isExistPerfilAndModulo(permissao);                
                if (exist) {                    
                    Permissao p = dao.getPermissao(permissao.perfil, permissao.modulo);                    
                    dao.update(p);
                }else {
                    dao.insert(permissao);
                }
            }
        }

PermissaoDAO

public class PermissaoDAO : GenericDAO<Permissao> {

        public IList<Permissao> findAll() {
            ISession _session = DBConnect.openSession();
            IList<Permissao> list = _session.CreateQuery("FROM Permissao p")                
                .List<Permissao>();
            return list;
        }


        /** verifica se o perfil e o modulo estao adicionados */
        public Boolean isExistPerfilAndModulo(Permissao permissao) {
            ISession _session = DBConnect.openSession();
            IList<Permissao> list = _session.CreateQuery("FROM Permissao p WHERE p.perfil = :p AND p.modulo = :m")
                .SetParameter("p", permissao.perfil)
                .SetParameter("m", permissao.modulo)
                .List<Permissao>();
            if (list.Count > 0) {
                return true;
            }
            return false;
        }

        /** retorna a permissao */
        public Permissao getPermissao(Permissao permissao) {
            ISession _session = DBConnect.openSession();
            IList<Permissao> list = _session.CreateQuery("FROM Permissao p WHERE p.perfil = :p AND p.modulo = :m").SetMaxResults(1)
                .SetParameter("p", permissao.perfil)
                .SetParameter("m", permissao.modulo)
                .List<Permissao>();
            return list[0];
        }


    }
    
asked by anonymous 10.03.2016 / 23:17

1 answer

2

I've seen this happen when you try to work with the same object in different sessions .

Example: If you create a session to retrieve an object A, however when you save / update the object you create and use another session .

Try to implement so that only session is used to do the task of retrieving and saving that object, in the code below for example only a new session is created if it does not already exist.

 ...
            private ISessionFactory _sessionFactory;
            private ISession _sessao;

            public ISession OpenSession()
            {
                if (_sessao == null)
                {
                    _sessao = _sessionFactory.OpenSession();
                }

                return _sessao;
            }
...
    
11.03.2016 / 04:21