/// <summary> /// Prepare the given Criteria object, applying cache settings and/or /// a transaction timeout. /// </summary> /// <remarks> /// <para>Note that for NHibernate 1.2 this only works if the /// implementation is of the type CriteriaImpl, which should generally /// be the case. The SetFetchSize method is not available on the /// ICriteria interface /// </para> /// <para>This is a no-op for NHibernate 1.0.x since /// the SetFetchSize method is not on the ICriteria interface and /// the implementation class is has internal access. /// </para> /// <para>To remove the method completely for Spring's NHibernate 1.0 /// support while reusing code for NHibernate 1.2 would not be /// possible. So now this ineffectual operation is left in tact for /// NHibernate 1.0.2 support.</para> /// </remarks> /// <param name="criteria">The criteria object to prepare</param> public void PrepareCriteria(ICriteria criteria) { if (CacheQueries) { criteria.SetCacheable(true); if (QueryCacheRegion != null) { criteria.SetCacheRegion(QueryCacheRegion); } } if (FetchSize > 0) { //TODO see if we can optimize performance. //CriteriaImpl is internal in NH 1.0.x object[] args = new object[] { FetchSize }; try { Type t = TypeResolutionUtils.ResolveType("NHibernate.Impl.CriteriaImpl, NHibernate"); if (t != null) { t.InvokeMember("SetFetchSize", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, criteria, args); } } catch (TypeLoadException e) { log.Warn("Can't set FetchSize for ICriteria", e); } } if (MaxResults > 0) { criteria.SetMaxResults(MaxResults); } SessionFactoryUtils.ApplyTransactionTimeout(criteria, SessionFactory); }
/// <summary> /// 分页读取 /// </summary> /// <param name="cri"></param> /// <param name="startIndex"></param> /// <param name="maxRows"></param> /// <param name="sortedBy"></param> /// <returns></returns> protected IList <T> GetAll(NHibernate.ICriteria cri, int startIndex, int maxRows, string sortedBy) { cri.SetFirstResult(startIndex); if (maxRows > 0) { cri.SetMaxResults(maxRows); } if (!string.IsNullOrEmpty(sortedBy)) { string[] ss = sortedBy.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (ss.Length == 2 && ss[1].ToUpper() == "DESC") { cri.AddOrder(new NHibernate.Criterion.Order(ss[0], false)); } else { cri.AddOrder(new NHibernate.Criterion.Order(ss[0], true)); } } return(cri.List <T>()); }
public IList <SystemUser> GetByRestrictions(IDictionary <string, string> restrictions, string orderBy, bool ascending, int?limit) { using (ISession session = NHibernateHelper.OpenSession()) { NHibernate. ICriteria criteria = session.CreateCriteria <SystemUser>(); foreach (KeyValuePair <string, string> pair in restrictions) { criteria = criteria.Add(Restrictions.Eq(pair.Key, pair.Value)); } if (limit != null) { criteria = criteria.SetMaxResults((int)limit); } if (orderBy != null) { criteria = criteria.AddOrder(new Order(orderBy, ascending)); } return(criteria.List <SystemUser>()); } }
/// <summary> /// /// </summary> /// <param name="criteria"></param> protected void OnCriteriaCreated(NHibernate.ICriteria criteria) { if (this.EagerFetchs != null) { foreach (string s in this.EagerFetchs) { criteria = criteria.SetFetchMode(s, FetchMode.Eager); } } if (EnablePage) { criteria = criteria.SetFirstResult(FirstResult); if (MaxResult != -1) { criteria = criteria.SetMaxResults(MaxResult); } } if (IsResultDistinct || m_hasCollection) { criteria = criteria.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer()); } }
/// <summary> /// Prepare the given Criteria object, applying cache settings and/or /// a transaction timeout. /// </summary> /// <remarks> /// <para>Note that for NHibernate 1.2 this only works if the /// implementation is of the type CriteriaImpl, which should generally /// be the case. The SetFetchSize method is not available on the /// ICriteria interface /// </para> /// <para>This is a no-op for NHibernate 1.0.x since /// the SetFetchSize method is not on the ICriteria interface and /// the implementation class is has internal access. /// </para> /// <para>To remove the method completely for Spring's NHibernate 1.0 /// support while reusing code for NHibernate 1.2 would not be /// possible. So now this ineffectual operation is left in tact for /// NHibernate 1.0.2 support.</para> /// </remarks> /// <param name="criteria">The criteria object to prepare</param> public void PrepareCriteria(ICriteria criteria) { if (CacheQueries) { criteria.SetCacheable(true); if (QueryCacheRegion != null) { criteria.SetCacheRegion(QueryCacheRegion); } } if (FetchSize > 0) { //TODO see if we can optimize performance. //CriteriaImpl is internal in NH 1.0.x object[] args = new object[] { FetchSize }; try { Type t = TypeResolutionUtils.ResolveType("NHibernate.Impl.CriteriaImpl, NHibernate"); if (t != null) { t.InvokeMember("SetFetchSize", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, criteria, args); } } catch (TypeLoadException e) { log.Warn("Can't set FetchSize for ICriteria", e); } } if (MaxResults > 0) { criteria.SetMaxResults(MaxResults); } SessionFactoryUtils.ApplyTransactionTimeout(criteria, SessionFactory); }