1. 使用Session.Delete(T t) 方法
使用这个方法,需要先从数据库中获取实体T或者直接new T(), 给Id赋值,然后才能调用 Session.Delete(T)
A: var session = NHibernateSession.Current; //自己写的一个获取Session的方法
var t = seesion.Get<Article>(999);
session.Delete(t);
B: var session = NHibernateSession.Current;
var t = new Article{ Id = 999};
session.Delete(t);
2. 使用Seesion.Delete(string query) 方法
var session = NHibernateSession.Current;
var hql = "from Article Where ArticleId = 999";
session.Delete(hql) ;
3. 使用IQuery.ExecuteUpdate() 方法
var session = NHibernateSession.Current;
var hql = "Delete Article Where ArticleId = 999";
session.CreateQuery(hql).ExecuteUpdate();
4. 使用ISQLQuery.ExecuteUpdate() 方法
var session = NHibernateSession.Current;
var sql = "Delete from Articles Where ArticleId = 999";
session.CreateSQLQuery(sql).ExecuteUpdate();
5. 使用Named Query
var session = NHibernateSession.Current;
session.GetNamedQuery("DeleteArticleUseSQLNamedQuery").ExecuteUpdate();
session.GetNamedQuery("DeleteArticleUseHQLNamedQuery").ExecuteUpdate();
NamedQuery.hbm.xml :
<?xml version="1.0" encoding="utf-8" ?>
<sql-query name="DeleteArticleUseSQLNamedQuery">
Delete from Articles Where ArticleId = 999
</sql-query>
<query name="DeleteArticleUseHQLNamedQuery">
Delete Article Where ArticleId = 999
</query>
</hibernate-mapping>
Nhibernate本来就支持批量删除,Nhibernate的删除的操作有4个重载的方法,
Delete(object obj),
Delete(string query),
Delete(string query,objetc value,NHibernate.Type.IType type),
Delete(string query,objetc[] values,NHibernate.Type.IType[] types),
后面三种方法都支持批量删除
如Delete(" from users where user_age >10"),就可以删除所有年纪大于10的用户,注意这是hql查询,users是实体对象的类名,而不是表名