示例#1
0
 //public object saveEntity(BaseEntity entity)
 //{
 //    object newentity = new object();
 //    if (entity.Id != 0)
 //    {
 //        //throw new Exception("id不为空,应使用update方法");
 //        // MessageBox.Show("保存失败!");
 //        return null;
 //    }
 //    ISessionFactory factory;
 //    ISession session = null;
 //    ITransaction transaction = null;
 //    // Tell NHibernate that this object should be updated
 //    try
 //    {
 //        factory = Connection.getConfiguration().BuildSessionFactory();
 //        session = factory.OpenSession();
 //        transaction = session.BeginTransaction();
 //        newentity = session.Save(session.Merge(entity));
 //        // commit all of the changes to the DB and close the ISession
 //        transaction.Commit();
 //        // MessageBox.Show("保存成功");
 //        return newentity;
 //    }
 //    catch (Exception e)
 //    {
 //        throw e;
 //        //if (transaction != null && transaction.IsActive)
 //        //{
 //        // transaction.Rollback();
 //        //}
 //        //MessageBox.Show("保存失败!");
 //    }
 //    finally
 //    {
 //        if (session != null && session.IsOpen)
 //        {
 //            session.Close();
 //        }
 //    }
 //}
 ///// <summary>
 /////删除实体 
 ///// </summary>
 ///// <param name="entity"></param>
 //public void deleteEntity(BaseEntity entity)
 //{
 //    if (entity.Id == 0)
 //    {
 //        //MessageBox.Show("删除失败!");
 //        return;
 //    }
 //    ISessionFactory factory;
 //    ISession session = null;
 //    ITransaction transaction = null;
 //    // Tell NHibernate that this object should be updated
 //    try
 //    {
 //        factory = Connection.getConfiguration().BuildSessionFactory();
 //        session = factory.OpenSession();
 //        transaction = session.BeginTransaction();
 //        session.Delete(session.Merge(entity));
 //        // commit all of the changes to the DB and close the ISession
 //        transaction.Commit();
 //        //MessageBox.Show("删除成功");
 //    }
 //    catch
 //    {
 //        if (transaction != null && transaction.IsActive)
 //        {
 //            transaction.Rollback();
 //        }
 //        //MessageBox.Show("删除失败!");
 //    }
 //    finally
 //    {
 //        if (session != null && session.IsOpen)
 //        {
 //            session.Close();
 //        }
 //    }
 //}
 /// <summary>
 /// 保存或更新实体
 /// </summary>
 /// <param name="entity"></param>
 public void SaveOrUpdateEntity(BaseEntity entity)
 {
     //ISessionFactory factory = null;
     ISession session = null;
     ITransaction transaction = null;
     // Tell NHibernate that this object should be updated
     try
     {
         //factory = Connection.getConfiguration().BuildSessionFactory();
         session = OpenSession();//factory.OpenSession();
         transaction = session.BeginTransaction();
         session.SaveOrUpdate(session.Merge(entity));
         // commit all of the changes to the DB and close the ISession
         transaction.Commit();
         //MessageBox.Show("保存成功");
     }
     catch (Exception e)
     {
         if (transaction != null && transaction.IsActive)
         {
             transaction.Rollback();
         }
         throw e;
     }
     finally
     {
         if (session != null && session.IsOpen)
         {
             factory.Close();
             session.Clear();
             session.Close();
             factory.Dispose();
             session.Dispose();
         }
     }
 }
示例#2
0
        public BaseEntity loadEntity(BaseEntity entity, long id)
        {
            if (id == 0)
            {
                return null;
            }
            try
            {

                ISession session = null;
                //ISessionFactory factory = null;

                //if (factory == null)

                session = OpenSession();//factory.OpenSession();

                ITransaction transaction = session.BeginTransaction();
                session.Load(entity, id);
                session.Close();
                return entity;
            }
            catch
            {
                return null;
            }
        }