public void deleteEntity(BaseEntity entity) { if (entity.Id == 0) { throw new Exception("id为空"); } ISessionFactory factory = Connection.Connection.getConfiguration().BuildSessionFactory(); ISession session = factory.OpenSession(); ITransaction transaction = session.BeginTransaction(); // Tell NHibernate that this object should be deleted try { session.Delete(entity); // commit all of the changes to the DB and close the ISession transaction.Commit(); MessageBox.Show("删除成功"); } catch(Exception e) { transaction.Rollback(); MessageBox.Show("删除失败:" + e.Message); } finally { session.Close(); } }
public BaseEntity loadEntity(BaseEntity entity, long id) { if (id == 0) { throw new Exception("id为空"); } ISessionFactory factory = Connection.Connection.getConfiguration().BuildSessionFactory(); ISession session = factory.OpenSession(); ITransaction transaction = session.BeginTransaction(); session.Load(entity, id); session.Close(); return entity; }
/// <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.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; ISession session=null; ITransaction transaction =null; // Tell NHibernate that this object should be updated try { factory = Connection.Connection.getConfiguration().BuildSessionFactory(); session = 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) { session.Close(); } } }
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.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(); } } }
public BaseEntity loadEntity(BaseEntity entity, long id) { if (id == 0) { return null; } try { ISessionFactory factory = Connection.Connection.getConfiguration().BuildSessionFactory(); ISession session = factory.OpenSession(); ITransaction transaction = session.BeginTransaction(); session.Load(entity, id); session.Close(); return entity; } catch { return null; } }