/// <summary> /// 刪除物件 /// </summary> /// <param name="id">主鍵</param> public void ExecuteDeleteById <T>(Object id) { //開啟連結 ISession session = HibernateHelper.GetCurrentSession(); try { HibernateHelper.BeginTransaction(); T temp = session.Load <T>(id); session.Delete(temp); HibernateHelper.CommitTransaction(); } //例外 catch (Exception e) { HibernateHelper.RollbackTransaction(); log.Error("刪除資料" + typeof(T) + "主鍵為:" + id + "失敗" + e.Message); throw e; } finally { //關閉 HibernateHelper.CloseSession(); } }
/// <summary> /// 新增 /// </summary> /// <param name="newo">物件</param> /// <returns>主鍵</returns> public Object ExecuteInsert(Object newo) { ISession session = HibernateHelper.GetCurrentSession(); ITransaction tx = HibernateHelper.BeginTransaction(); try { //儲存 Object o = session.Save(newo); HibernateHelper.CommitTransaction(); return(o); } //例外 catch { HibernateHelper.RollbackTransaction(); log.Error("新增" + newo + "失敗!"); throw; } //關閉 finally { HibernateHelper.CloseSession(); } }
//2009/06/25 dennis add 加入ExecuteAllDelete方法 /// <summary> /// 根據條件刪除資料 /// </summary> /// <param name="sql">查詢sql(from test a where a.Id='1' )</param> public void ExecuteAllDelete(string sql) { //開啟連結 ISession session = HibernateHelper.GetCurrentSession(); try { HibernateHelper.BeginTransaction(); //刪除 session.Delete(sql); HibernateHelper.CommitTransaction(); } //例外 catch (Exception e) { HibernateHelper.RollbackTransaction(); log.Error("刪除資料" + sql + "失敗" + e.Message); throw e; } finally { //關閉 HibernateHelper.CloseSession(); } }
/// <summary> /// 更新物件 /// </summary> /// <param name="o">待更新的物件</param> public void ExecuteUpdate(Object o) { ISession session = HibernateHelper.GetCurrentSession(); ITransaction tr = HibernateHelper.BeginTransaction(); try { session.Merge(o); HibernateHelper.CommitTransaction(); } //例外 catch { HibernateHelper.RollbackTransaction(); log.Error("更新" + o + "失敗!"); throw; } //關閉連結 finally { HibernateHelper.CloseSession(); } }