Just wanted to add that: a) You should almost always perform your changes inside a transaction. b) You do probably not want to sprinkle calls to Flush() in the Delete() method or other "low-level" methods. When NH performs a flush it will dirty-check all active objects known by the current NH session. Therefore flushing too often is bad for performance.
/Oskar 2014-04-22 4:12 GMT+02:00 Veasna MUCH <[email protected]>: > Dear RP, > > By the way, in Save(T entity) or SaveOrUpdate(T entity), why does the > record is save in the database without needed to applied Flush? > > Is that needed only for deleting record? > >> public void Delete(T entity) { >> NHibernateSession.Delete(entity); >> NHibernateSession.Flush(); >> } > > > > Best regards, > > Veasna > > > On Monday, April 21, 2014 6:16:03 PM UTC+7, Ricardo Peres wrote: >> >> This has to do with the FlushMode of the ISession. By default, you have >> to explicitly Flush the ISession manually, or, if you start and commit a >> transaction, it is done for you. >> This is by design, pretty much like Entity Framework, where you have to >> call SaveChanges. >> >> RP >> >> On Monday, April 21, 2014 11:52:04 AM UTC+1, Veasna MUCH wrote: >>> >>> I am very new to NHibernate. I try to use some NH methods. I am able >>> to get all records in grid, save new records but no able to delete the >>> records from db. Not even getting any kind of exception.. >>> using the method as NHibernateSession.Delete(entity); //entity is >>> retrieved using GetByAirportCode("A1") //A1 is an code of the record >>> that I want to delete. I get object with Code = "A1" but it does not delete >>> that record from DB. When I added the code *objAirportDao.CommitChanges(); >>> *the record is deleted from DB. I do no understand why do I need to >>> apply commitchange here? It should be automatically right? if that's the >>> need why does the Saving record does not need to applied CommitChange? >>> >>> >>> >>> Here is where I call delete object in *default.aspx.cs* >>> >>> >>> >>> protected void Button1_Click(object sender, EventArgs e) { >>>> >>> IRepositoryFactory daoFactory = new >>> NHibernateDaoFactory(); >>> >>>> IAirportRepository objAirportDao = >>>> daoFactory.GetAirportDao(); >>>> Airport objAirport = objAirportDao.GetByAirportCode ("A1"); >>> >>> if (objAirport != null){ >>>> >>>> * objAirportDao.Delete(objAirport); >>>> //objAirportDao.CommitChanges(); * >>>> } >>> >>> >>>> } >>> >>> >>> >>> >>> >>> *Class AirportRepository * >>> >>> >>> >>> >>>> >>>> >>>> >>>> >>>> >>>> * public class AirportRepository : >>>> AbstractNHibernateRepository<Airport, int>, IAirportRepository { >>>> public AirportRepository() { } public Airport >>>> GetByAirportCode(string airportCode) { return GetByCriteria(new >>>> ICriterion[] { Expression.Eq("AirportCode", airportCode >>>> )}).ToList<Airport>().FirstOrDefault(); } }* >>> >>> >>> >>> *Class NHibernateSessionFactory* >>> >>> public class NHibernateSessionFactory{ >>>> public static NHibernateSessionFactory Instance >>>> { >>>> get { >>>> return Nested.NHibernateSessionFactory; >>>> } >>>> } >>>> >>>> private NHibernateSessionFactory(){ >>>> InitSessionFactory(); >>>> } >>>> >>>> private class Nested { >>>> static Nested() { } >>>> internal static readonly NHibernateSessionFactory >>>> NHibernateSessionFactory = new NHibernateSessionFactory(); >>>> } >>>> >>>> private void InitSessionFactory(){ >>>> sessionFactory = new Configuration().Configure(). >>>> BuildSessionFactory(); >>>> } >>>> >>>> public void RegisterInterceptor(IInterceptor interceptor) { >>>> ISession session = ContextSession; >>>> if (session != null && session.IsOpen) >>>> { >>>> throw new CacheException("You cannot register an >>>> interceptor once a session has already been opened"); >>>> } >>>> GetSession(interceptor); >>>> } >>>> public ISession GetSession(){ >>>> return GetSession(null); >>>> } >>>> >>>> private ISession GetSession(IInterceptor interceptor) { >>>> ISession session = ContextSession; >>>> if (session == null){ >>>> if (interceptor != null){ >>>> session = sessionFactory.OpenSession(interceptor); >>>> }else{ >>>> session = sessionFactory.OpenSession(); >>>> } >>>> ContextSession = session; >>>> } >>>> return session; >>>> } >>>> >>>> public void CloseSession(){ >>>> ISession session = ContextSession; >>>> if (session != null && session.IsOpen){ >>>> session.Flush(); >>>> session.Close(); >>>> } >>>> ContextSession = null; >>>> } >>>> >>> >>> >>>> public void BeginTransaction() { >>>> ITransaction transaction = ContextTransaction; >>>> if (transaction == null){ >>>> transaction = GetSession().BeginTransaction(); >>>> ContextTransaction = transaction; >>>> } >>>> } >>>> >>> >>> >>>> public void CommitTransaction(){ >>>> ITransaction transaction = ContextTransaction; >>>> try >>>> { >>>> if (HasOpenTransaction()) { >>>> transaction.Commit(); >>>> ContextTransaction = null; >>>> } >>>> } catch (HibernateException){ >>>> RollbackTransaction(); >>>> throw; >>>> } >>>> } >>>> >>> >>> >>>> public bool HasOpenTransaction(){ >>>> ITransaction transaction = ContextTransaction; >>>> return transaction != null && !transaction.WasCommitted && >>>> !transaction.WasRolledBack; >>>> } >>>> >>> >>> >>>> public void RollbackTransaction(){ >>>> ITransaction transaction = ContextTransaction; >>>> try >>>> { >>>> if (HasOpenTransaction()){ >>>> transaction.Rollback(); >>>> } >>>> ContextTransaction = null; >>>> } >>>> finally >>>> { >>>> CloseSession(); >>>> } >>>> } >>>> >>>> private ITransaction ContextTransaction{ >>>> get >>>> { >>>> if (IsInWebContext()){ >>>> return (ITransaction)HttpContext. >>>> Current.Items[TRANSACTION_KEY]; >>>> }else{ >>>> return (ITransaction)CallContext. >>>> GetData(TRANSACTION_KEY); >>>> } >>>> } >>>> set >>>> { >>>> if (IsInWebContext()){ >>>> HttpContext.Current.Items[TRANSACTION_KEY] = value; >>>> }else{ >>>> CallContext.SetData(TRANSACTION_KEY, value); >>>> } >>>> } >>>> } >>>> >>>> private ISession ContextSession{ >>>> get >>>> { >>>> if (IsInWebContext()) >>>> { >>>> return (ISession)HttpContext.Current. >>>> Items[SESSION_KEY]; >>>> } >>>> else >>>> { >>>> return (ISession)CallContext.GetData(SESSION_KEY); >>>> } >>>> } >>>> set >>>> { >>>> if (IsInWebContext()) >>>> { >>>> HttpContext.Current.Items[SESSION_KEY] = value; >>>> } >>>> else >>>> { >>>> CallContext.SetData(SESSION_KEY, value); >>>> } >>>> } >>>> } >>>> >>> >>> >>>> private bool IsInWebContext() { >>>> return HttpContext.Current != null; >>>> } >>>> private const string TRANSACTION_KEY = "CONTEXT_TRANSACTION"; >>>> private const string SESSION_KEY = "CONTEXT_SESSION"; >>>> private ISessionFactory sessionFactory; >>>> } >>> >>> >>> >>> >>> >>> *Here is my abstract class AbstractNHibernateRepository:* >>> >>> >>> public abstract class AbstractNHibernateRepository<T, IdT> : >>>> IRepository<T, IdT> { >>>> >>>> >>>> >>>> public T GetById(IdT id, bool shouldLock){ >>>> T entity; >>>> if (shouldLock) { >>>> entity = (T)NHibernateSession.Load(persitentType, id, >>>> LockMode.Upgrade); >>>> } else { >>>> entity = (T)NHibernateSession.Load(persitentType, id); >>>> } >>>> return entity; >>>> } >>>> >>>> public List<T> GetAll() { >>>> return GetByCriteria(); >>>> } >>>> >>>> public List<T> GetByCriteria(params ICriterion[] criterion) { >>>> ICriteria criteria = NHibernateSession. >>>> CreateCriteria(persitentType); >>>> foreach (ICriterion criterium in criterion) >>>> { >>>> criteria.Add(criterium); >>>> } >>>> return criteria.List<T>() as List<T>; >>>> } >>>> >>>> public List<T> GetByExample(T exampleInstance, params string[] >>>> propertiesToExclude) { >>>> ICriteria criteria = NHibernateSession. >>>> CreateCriteria(persitentType); >>>> Example example = Example.Create(exampleInstance); >>>> foreach (string propertyToExclude in propertiesToExclude) { >>>> example.ExcludeProperty(propertyToExclude); >>>> } >>>> criteria.Add(example); >>>> return criteria.List<T>() as List<T>; >>>> } >>>> >>>> >>>> >>> >>>> public T GetUniqueByExample(T exampleInstance, params string[] >>>> propertiesToExclude) { >>>> List<T> foundList = GetByExample(exampleInstance, >>>> propertiesToExclude); >>>> if (foundList.Count > 1) { >>>> throw new NonUniqueResultException(foundList.Count); >>>> } >>>> if (foundList.Count > 0){ >>>> return foundList[0]; >>>> } else { >>>> return default(T); >>>> } >>>> } >>>> >>>> >>>> >>> >>>> public T Save(T entity){ >>>> NHibernateSession.Save(entity); >>>> return entity; >>>> } >>>> >>>> public T SaveOrUpdate(T entity) { >>>> NHibernateSession.SaveOrUpdate(entity); >>>> return entity; >>>> } >>>> >>>> >>> >>>> public void Delete(T entity){ >>>> NHibernateSession.Delete(entity); >>>> } >>>> >>>> public void CommitChanges() { >>>> if (NHibernateSessionFactory.Instance.HasOpenTransaction()) >>>> { >>>> NHibernateSessionFactory.Instance.CommitTransaction(); >>>> } else{ >>>> NHibernateSessionFactory.Instance.GetSession().Flush(); >>>> } >>>> } >>>> >>>> private ISession NHibernateSession{ >>>> get { >>>> return NHibernateSessionFactory.Instance.GetSession(); >>>> } >>>> } >>>> private Type persitentType = typeof(T); >>>> } >>>> >>>> >>> >>> >>> >>> -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/nhusers. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/nhusers. For more options, visit https://groups.google.com/d/optout.
