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: What does Session.Clear() do?
PostPosted: Mon Aug 18, 2008 2:09 pm 
Newbie

Joined: Mon Jun 23, 2008 6:38 pm
Posts: 13
Code between sessionFactory.openSession() and session.close():

I am not sure what session.clear() does and when I need that? If I removed the bolded line (session.clear();) from the code below, nhibernate would try updating the entity when GetRelatedEmployers is called. Since i restrict the nhibernate user to update in the DB, it will throw an error. However, everything is fine if I leave the session.clear() there. Could someone explain what session.clear does please? Thanks.

Code:
List<Employer> employers = null;
using(EmployerDAO thisDAO = new Employer(123)){
      employers  = thisDAO.GetRelatedEmployers();
}



Code:
public class EmployerDAO : IDisposable
    {
        private Employer thisEmployer;
        private ISession session;

        public Employer ThisEmployer
        {
            get { return thisCompany; }           
        }

        public EmployerDAO (int employerId)
        {
            try
            {
                session = NHibernateSessionHelper.GetCurrentSession();
                if (CoId > 0)
                {
                    thisCompany = session.Get<Employer >(employerId);
                    if (thisCompany == null)
                    {
                        throw new NotFoundEmployerException(CoId);
                    }

                    //There is a problem if I don't clear the session.
                   session.Clear();
                }
            }
            catch { throw; }
            finally { this.Dispose(); }
        }

        public List<Employer> GetRelatedEmployers()
        {

            IList<Employer> relatedEmployers = null;
            if (thisEmployer.type!= 0)
            {

                ICriteria criteria = session.CreateCriteria(typeof(Employer), "employer")
                .Add(Expression.Eq("type",thisEmployer.type));

                relatedEmployers = criteria.SetMaxResults(4)
                                    .List<Employer>();

            }

            if (relatedEmployers == null)
            {
                return new List<Employer>();
            }
            else
            {
                return new List<Employer>(relatedEmployers );
            }

        }

#region IDisposable Members

        public void Dispose()
        {
            if (session.IsOpen)
                session.Close();
        }

        #endregion
}


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.