How can one delete NHibernate objects using a criteria?

前端 未结 4 1361
旧巷少年郎
旧巷少年郎 2020-12-15 23:28

This must be a simple question. Given a criteria, how one deletes the entities satisfying the criteria?

The rationale:

HQL and NH criteria

4条回答
  •  遥遥无期
    2020-12-16 00:18

    In your repository/dao/persistencemanager/whatever class:

    public IEnumerable FindAll(DetachedCriteria criteria)
    
            {
    
                return criteria.GetExecutableCriteria(Session).List();
    
            }
    

    and then

    public void Delete(DetachedCriteria criteria)
    
            {
    
                foreach (T entity in FindAll(criteria))
    
                {
    
                    Delete(entity);
    
                }
    
            }
    

    See Davy Brion's post Data Access with NHibernate.

    Edit:

    As far as I know, if you want to use Criteria you need to load the objects and iterate over them to delete them. Alternatively use HQL or pass in the SQL to the session.

提交回复
热议问题