using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate;
using Spring.Data.NHibernate;
using Spring.Data.NHibernate.Generic;
using NHibernate.Criterion;
using Spring.Data.NHibernate.Support;
namespace Utility
{
public class HibernateGet : IHibernateCallback
{
private Type type;
private object primaryKey;
public HibernateGet(Type type, object primaryKey)
{
this.type = type;
this.primaryKey = primaryKey;
}
public object DoInHibernate(ISession session)
{
return session.Get(type, primaryKey);
}
}
public class HibernateGetAggValue<T> : IHibernateCallback
{
private ProjectionList projectionList;
private ICriterion[] criterions;
private Type type;
public HibernateGetAggValue(ProjectionList projectionList, ICriterion[] criterions, Type type)
{
this.projectionList = projectionList;
this.criterions = criterions;
this.type = type;
}
public object DoInHibernate(ISession session)
{
ICriteria crit = session.CreateCriteria(type);
for ( int i = 0; i < criterions.Length; i++)
{
crit.Add(criterions[i]);
}
crit.SetProjection(projectionList);
return crit.List<T>();
}
}
public class HibernateGetAll<T> : IHibernateCallback
{
private Type type;
public HibernateGetAll(Type type)
{
this.type = type;
}
public object DoInHibernate(ISession session)
{
return session.CreateCriteria(type).List<T>();
}
}
public class HibernateSave<T, PK> : IHibernateCallback
{
private T entity;
private PK pk;
public HibernateSave(T t)
{
this.entity = t;
}
public object DoInHibernate(ISession session)
{
session.FlushMode = FlushMode.Auto;
pk = (PK)session.Save(entity);
session.Flush();
return pk;
}
}
public class HibernateUpdate<T> : IHibernateCallback
{
private T entity;
public HibernateUpdate(T entity)
{
this.entity = entity;
}
public object DoInHibernate(ISession session)
{
session.FlushMode = FlushMode.Auto;
session.Update(entity);
session.Flush();
return null;
}
}
public class HibernateSaveOrUpdate<T> : IHibernateCallback
{
private T entity;
public HibernateSaveOrUpdate(T entity)
{
this.entity = entity;
}
public object DoInHibernate(ISession session)
{
session.FlushMode = FlushMode.Auto;
session.SaveOrUpdate(entity);
session.Flush();
return null;
}
}
public class HibernateDelete<T> : IHibernateCallback
{
private T entity;
public HibernateDelete(T entity)
{
this.entity = entity;
}
public object DoInHibernate(ISession session)
{
session.FlushMode = FlushMode.Auto;
session.Delete(entity);
session.Flush();
return null;
}
}
public class HibernateFindByCriteria<T> : IHibernateCallback
{
private ICriterion[] Criterions;
private Type type;
public HibernateFindByCriteria(ICriterion[] Criterions, Type type)
{
this.Criterions = Criterions;
this.type = type;
}
public object DoInHibernate(ISession session)
{
ICriteria crit = session.CreateCriteria(type);
for ( int i = 0; i < Criterions.Length; i++)
{
crit.Add(Criterions[i]);
}
return crit.List<T>();
}
}
public class HibernateGetListForPage<T> : IHibernateCallback
{
private ICriterion[] criterions;
private Type type;
private int offset, length;
public HibernateGetListForPage(ICriterion[] criterions, Type
type, int offset, int length)
{
this.criterions = criterions;
this.type = type;
this.offset = offset;
this.length = length;
}
public object DoInHibernate(ISession session)
{
ICriteria criteria = session.CreateCriteria(type);
// 循环遍历添加约束条件
for ( int i = 0; i < criterions.Length; i++)
{
criteria.Add(criterions[i]);
}
criteria.SetFirstResult(offset);
criteria.SetMaxResults(length);
return criteria.List<T>();
}
}
public class HibernateFindByCriterias<T> : IHibernateCallback
{
private Order order;
private ICriterion[] criterions;
private Type type;
public HibernateFindByCriterias(Order order, ICriterion[] criterions, Type type)
{
this.criterions = criterions;
this.order = order;
this.type = type;
}
public object DoInHibernate(ISession session)
{
ICriteria crit = session.CreateCriteria(type);
for ( int i = 0; i < criterions.Length; i++)
{
crit.Add(criterions[i]);
}
if (order != null)
crit.AddOrder(order);
return crit.List<T>();
}
}
public class HibernateFindByCriteriass<T> : IHibernateCallback
{
private int firstResult;
private int rowCount;
private Order order;
private ICriterion[] criterion;
private Type type;
public HibernateFindByCriteriass( int firstResult, int rowCount, Order order, Type type, ICriterion[] criterion)
{
this.firstResult = firstResult;
this.rowCount = rowCount;
this.order = order;
this.criterion = criterion;
this.type = type;
}
public object DoInHibernate(ISession session)
{
ICriteria crit = session.CreateCriteria(type);
for ( int i = 0; i < criterion.Length; i++)
{
crit.Add(criterion[i]);
}
if (order != null)
crit.AddOrder(order);
crit.SetFirstResult(firstResult);
crit.SetMaxResults(rowCount);
return crit.List<T>();
}
}
}