public void MultiCriteriaQueriesWithIntsShouldExecuteCorrectly() { var driver = Sfi.ConnectionProvider.Driver; if (!driver.SupportsMultipleQueries) { Assert.Ignore("Driver {0} does not support multi-queries", driver.GetType().FullName); } // Test querying IntData using (ISession session = this.OpenSession()) { ICriteria criteriaWithPagination = session.CreateCriteria <DomainClass>(); criteriaWithPagination.Add(Expression.Le("IntData", 2)); ICriteria criteriaWithRowCount = CriteriaTransformer.Clone(criteriaWithPagination); criteriaWithPagination.SetFirstResult(0).SetMaxResults(1); criteriaWithRowCount.SetProjection(Projections.RowCountInt64()); IMultiCriteria multiCriteria = session.CreateMultiCriteria(); multiCriteria.Add(criteriaWithPagination); multiCriteria.Add(criteriaWithRowCount); IList results = multiCriteria.List(); long numResults = (long)((IList)results[1])[0]; IList list = (IList)results[0]; Assert.AreEqual(2, criteriaWithRowCount.UniqueResult <long>()); Assert.AreEqual(1, list.Count); } }
public void CanNotRetrieveDetachedCriteriaResultWithUnknownKey() { CreateItems(); using (ISession session = OpenSession()) { IMultiCriteria multiCriteria = session.CreateMultiCriteria(); DetachedCriteria firstCriteria = DetachedCriteria.For(typeof(Item)) .Add(Expression.Lt("id", 50)); multiCriteria.Add("firstCriteria", firstCriteria); try { IList firstResult = (IList)multiCriteria.GetResult("unknownKey"); Assert.Fail("This should've thrown an InvalidOperationException"); } catch (InvalidOperationException) { } catch (Exception) { Assert.Fail("This should've thrown an InvalidOperationException"); } } RemoveAllItems(); }
public void CanExecuteMultiplyQueriesInSingleRoundTrip() { using (ISession s = OpenSession()) { Item item = new Item(); item.Id = 1; s.Save(item); s.Flush(); } using (ISession s = OpenSession()) { ICriteria getItems = s.CreateCriteria(typeof(Item)); ICriteria countItems = s.CreateCriteria(typeof(Item)) .SetProjection(Projections.RowCount()); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(getItems) .Add(countItems); IList results = multiCriteria.List(); IList items = (IList)results[0]; Item fromDb = (Item)items[0]; Assert.AreEqual(1, fromDb.Id); IList counts = (IList)results[1]; int count = (int)counts[0]; Assert.AreEqual(1, count); } using (ISession s = OpenSession()) { s.Delete("from Item"); s.Flush(); } }
public void CanNotAddDetachedCriteriaWithKeyThatAlreadyExists() { using (ISession session = OpenSession()) { IMultiCriteria multiCriteria = session.CreateMultiCriteria(); DetachedCriteria firstCriteria = DetachedCriteria.For(typeof(Item)) .Add(Expression.Lt("id", 50)); DetachedCriteria secondCriteria = DetachedCriteria.For(typeof(Item)); multiCriteria.Add("firstCriteria", firstCriteria); try { multiCriteria.Add("firstCriteria", secondCriteria); Assert.Fail("This should've thrown an InvalidOperationException"); } catch (InvalidOperationException) { } catch (Exception) { Assert.Fail("This should've thrown an InvalidOperationException"); } } }
public void TwoMultiQueriesWithDifferentPagingGetDifferentResultsWhenUsingCachedQueries() { CreateItems(); using (ISession s = OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(10)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(89, items.Count); int count = (int)((IList)results[1])[0]; Assert.AreEqual(99L, count); } using (ISession s = OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(20)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(79, items.Count, "Should have gotten different result here, because the paging is different"); int count = (int)((IList)results[1])[0]; Assert.AreEqual(99L, count); } RemoveAllItems(); }
public IPagedList <Album> GetAlbums(ILoadOptions options) { IPagedList <Album> result = new PagedList <Album>(); if (options == null) { return(result); } if (options.MaxResults <= 0) { return(result); } ISession session = SessionFactory.GetSession(); try { DetachedCriteria countCriteria = GetAlbumsImpl(options); DetachedCriteria listCriteria = GetAlbumsImpl(options); countCriteria.SetProjection(Projections.RowCount()); countCriteria.ClearOrders(); listCriteria. SetFirstResult(options.FirstResult). SetMaxResults(options.MaxResults); IMultiCriteria multiCriteria = session.CreateMultiCriteria(); multiCriteria.Add(countCriteria); multiCriteria.Add(listCriteria); IList queryResult = multiCriteria.List(); result.TotalItems = (int)((IList)queryResult[0])[0]; IList recordsList = (IList)queryResult[1]; EntityConverter entityConverter = new EntityConverter(); foreach (var e in recordsList) { AlbumEntity dataEntity = e as AlbumEntity; Album businessEntity = entityConverter.FromDataEntity(dataEntity, AlbumConvertOptions.Small); result.Add(businessEntity); } } catch (Exception ex) { Logger.Write(ex); } finally { session.Close(); } return(result); }
/// <summary> /// To use multi criteria to eagerly load multiple child collection, if the first detached criteria has child collection to query, /// then must use .SetResultTransformer(new DistinctRootEntityResultTransformer() for the first detached criteria /// </summary> /// <typeparam name="TRoot">The type of the root.</typeparam> /// <param name="multiCriteria">The multi criteria.</param> /// <param name="rootCriterion">The root criterion.</param> /// <param name="associationPropertyExpression">The association property expression.</param> /// <param name="orders">The orders.</param> /// <param name="returnDistinctRoot">If set to <c>true</c> [return distinct root].</param> /// <returns>A <see cref="IMultiCriteria"/></returns> public static IMultiCriteria AddDetachedCriteriaForChild <TRoot> ( this IMultiCriteria multiCriteria, ICriterion rootCriterion, Expression <Func <TRoot, object> > associationPropertyExpression, IEnumerable <Order> orders = null, bool returnDistinctRoot = false) { var detachedCriteria = DetachedCriteriaUtil.CreateDetachedCriteriaForChild( rootCriterion, associationPropertyExpression); return(multiCriteria.AddDetachedCriteria(detachedCriteria, orders, returnDistinctRoot)); }
/// <summary> /// Set a timeout for the underlying ADO.NET query. /// </summary> /// <param name="timeout">The timeout in seconds.</param> /// <param name="multiCriteria">The <see cref="IMultiCriteria" /> on which to set the timeout.</param> /// <returns><paramref name="multiCriteria" /> (for method chaining).</returns> public static IMultiCriteria SetTimeout(this IMultiCriteria multiCriteria, int timeout) { if (multiCriteria == null) { throw new ArgumentNullException(nameof(multiCriteria)); } if (multiCriteria is MultiCriteriaImpl impl) { return(impl.SetTimeout(timeout)); } throw new NotSupportedException(multiCriteria.GetType() + " does not support SetTimeout"); }
/// <summary> /// To use multi criteria to eagerly load multiple child collection, if the first detached criteria has child collection to query, /// then must use .SetResultTransformer(new DistinctRootEntityResultTransformer() for the first detached criteria /// </summary> /// <param name="multiCriteria">The multi criteria.</param> /// <param name="detachedCriteria">The detached criteria.</param> /// <param name="orders">The orders.</param> /// <param name="returnDistinctRoot">If set to <c>true</c> [return distinct root].</param> /// <returns>A <see cref="IMultiCriteria"/></returns> public static IMultiCriteria AddDetachedCriteria( this IMultiCriteria multiCriteria, DetachedCriteria detachedCriteria, IEnumerable <Order> orders = null, bool returnDistinctRoot = false) { if (orders != null && orders.Count() > 0) { detachedCriteria.AddOrders(orders); } if (returnDistinctRoot) { detachedCriteria = detachedCriteria.SetResultTransformer(new DistinctRootEntityResultTransformer()); } multiCriteria.Add(detachedCriteria); return(multiCriteria); }
public void CanGetResultInAGenericList() { using (ISession s = OpenSession()) { ICriteria getItems = s.CreateCriteria(typeof(Item)); ICriteria countItems = s.CreateCriteria(typeof(Item)) .SetProjection(Projections.RowCount()); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(getItems) // we expect a non-generic result from this (ArrayList) .Add <int>(countItems); // we expect a generic result from this (List<int>) IList results = multiCriteria.List(); Assert.That(results[0], Is.InstanceOf <ArrayList>()); Assert.That(results[1], Is.InstanceOf <List <int> >()); } }
private void DoMutiQueryAndAssert() { using (ISession s = OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(10)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(89, items.Count); int count = (int)((IList)results[1])[0]; Assert.AreEqual(99L, count); } }
public IList <TD> GetPageEntites(IList <ICriterion> expressions, IList <Order> orders, int pageSize, int page, out long count) { using (var session = GetSession()) { using (var trans = session.BeginTransaction()) { IMultiCriteria mulicrit = session.CreateMultiCriteria(); ICriteria queryCrit = session.CreateCriteria(typeof(TD)); ICriteria countCrit = session.CreateCriteria(typeof(TD)); foreach (var exp in expressions) { queryCrit.Add(exp); countCrit.Add(exp); } foreach (var order in orders) { queryCrit.AddOrder(order); } IList results = mulicrit .Add(queryCrit.SetFirstResult(page * pageSize).SetMaxResults(pageSize)) .Add(countCrit.SetProjection(Projections.RowCountInt64())) .List(); trans.Commit(); IList <TD> _list = new List <TD>(); foreach (var o in (IList)results[0]) { _list.Add((TD)o); } count = (long)((IList)results[1])[0]; return(_list); } } }
public virtual IList Execute(ISession theSession) { // a sub class may need this at a later date. session = theSession; if (criteriaList.Count == 0) { return(new ArrayList()); } IMultiCriteria multiCriteria = theSession.CreateMultiCriteria(); foreach (DetachedCriteria detachedCriteria in criteriaList) { multiCriteria.Add(CreateCriteria(theSession, detachedCriteria)); } IList list = multiCriteria.List(); int results = list.Count; for (int i = 0; i < results; i++) { if (collectionDelegates[i] != null) { collectionDelegates[i]((IList)list[i]); } if (uniqueResultDelegate[i] != null) { object single = Collection.Single((ICollection)list[i]); uniqueResultDelegate[i](single); } if (collectionAndCountDelegate[i] != null) { IList queryList = (IList)list[i]; int count = Convert.ToInt32( Collection.Single((ICollection)list[i + 1]) ); collectionAndCountDelegate[i](queryList, count); i += 1;//not a best practice, I will admit } } return(list); }
public void ExecutingCriteriaThroughMultiCriteriaTransformsResults() { CreateItems(); using (ISession session = OpenSession()) { ResultTransformerStub transformer = new ResultTransformerStub(); ICriteria criteria = session.CreateCriteria(typeof(Item)) .SetResultTransformer(transformer); IMultiCriteria multiCriteria = session.CreateMultiCriteria() .Add(criteria); multiCriteria.List(); Assert.IsTrue(transformer.WasTransformTupleCalled, "Transform Tuple was not called"); Assert.IsTrue(transformer.WasTransformListCalled, "Transform List was not called"); } RemoveAllItems(); }
public void Test() { IDriver driver = sessions.ConnectionProvider.Driver; if (!driver.SupportsMultipleQueries) { Assert.Ignore("Driver {0} does not support multi-queries", driver.GetType().FullName); } using (ISession session = sessions.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { EntityA a1 = CreateEntityA(session); EntityA a2 = CreateEntityA(session); EntityC c = CreateEntityC(session); EntityB b = CreateEntityB(session, a1, c); // make sure the created entities are no longer in the session session.Clear(); IMultiCriteria multi = session.CreateMultiCriteria(); // the first query is a simple select by id on EntityA multi.Add(session.CreateCriteria(typeof(EntityA)).Add(Restrictions.Eq("Id", a1.Id))); // the second query is also a simple select by id on EntityB multi.Add(session.CreateCriteria(typeof(EntityA)).Add(Restrictions.Eq("Id", a2.Id))); // the final query selects the first element (using SetFirstResult and SetMaxResults) for each EntityB where B.A.Id = a1.Id and B.C.Id = c.Id // the problem is that the paged query uses parameters @p0 and @p1 instead of @p2 and @p3 multi.Add( session.CreateCriteria(typeof(EntityB)).Add(Restrictions.Eq("A.Id", a1.Id)).Add(Restrictions.Eq("C.Id", c.Id)). SetFirstResult(0).SetMaxResults(1)); IList results = multi.List(); Assert.AreEqual(1, ((IList)results[0]).Count); Assert.AreEqual(1, ((IList)results[1]).Count); Assert.AreEqual(1, ((IList)results[2]).Count); } } }
/// <summary> /// Finds all by multi criteria. /// </summary> /// <param name="detachedCriteriaDic">The detached criteria dic.</param> /// <returns></returns> public Dictionary <string, IList> FindAllByMultiCriteria(IDictionary <string, DetachedCriteria> detachedCriteriaDic) { Dictionary <string, IList> resultDict = new Dictionary <string, IList>(); if (detachedCriteriaDic != null && detachedCriteriaDic.Keys != null && detachedCriteriaDic.Keys.Count > 0) { IMultiCriteria multicriteria = Session.CreateMultiCriteria(); foreach (string key in detachedCriteriaDic.Keys) { multicriteria.Add(detachedCriteriaDic[key]); } IList list = multicriteria.List(); int count = 0; foreach (string key in detachedCriteriaDic.Keys) { resultDict.Add(key, (IList)list[count]); count++; } } return(resultDict); }
public void MultiCriteriaQueriesWithIntsShouldExecuteCorrectly() { // Test querying IntData using (ISession session = this.OpenSession()) { ICriteria criteriaWithPagination = session.CreateCriteria <DomainClass>(); criteriaWithPagination.Add(Expression.Le("IntData", 2)); ICriteria criteriaWithRowCount = CriteriaTransformer.Clone(criteriaWithPagination); criteriaWithPagination.SetFirstResult(0).SetMaxResults(1); criteriaWithRowCount.SetProjection(Projections.RowCountInt64()); IMultiCriteria multiCriteria = session.CreateMultiCriteria(); multiCriteria.Add(criteriaWithPagination); multiCriteria.Add(criteriaWithRowCount); IList results = multiCriteria.List(); long numResults = (long)((IList)results[1])[0]; IList list = (IList)results[0]; Assert.AreEqual(2, criteriaWithRowCount.UniqueResult <long>()); Assert.AreEqual(1, list.Count); } }
public void CanGetMultiQueryFromSecondLevelCache() { CreateItems(); //set the query in the cache DoMutiQueryAndAssert(); Hashtable cacheHashtable = GetHashTableUsedAsQueryCache(); IList cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0]; IList cachedQuery = (IList)cachedListEntry[1]; IList firstQueryResults = (IList)cachedQuery[0]; firstQueryResults.Clear(); firstQueryResults.Add(3); firstQueryResults.Add(4); IList secondQueryResults = (IList)cachedQuery[1]; secondQueryResults[0] = 2; using (ISession s = sessions.OpenSession()) { ICriteria criteria = s.CreateCriteria(typeof(Item)) .Add(Expression.Gt("id", 50)); IMultiCriteria multiCriteria = s.CreateMultiCriteria() .Add(CriteriaTransformer.Clone(criteria).SetFirstResult(10)) .Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount())); multiCriteria.SetCacheable(true); IList results = multiCriteria.List(); IList items = (IList)results[0]; Assert.AreEqual(2, items.Count); int count = (int)((IList)results[1])[0]; Assert.AreEqual(2L, count); } RemoveAllItems(); }
public void CanAddDetachedCriteriaWithKeyAndRetrieveResultsWithKey() { CreateItems(); using (ISession session = OpenSession()) { IMultiCriteria multiCriteria = session.CreateMultiCriteria(); DetachedCriteria firstCriteria = DetachedCriteria.For(typeof(Item)) .Add(Expression.Lt("id", 50)); DetachedCriteria secondCriteria = DetachedCriteria.For(typeof(Item)); multiCriteria.Add("firstCriteria", firstCriteria); multiCriteria.Add("secondCriteria", secondCriteria); IList secondResult = (IList)multiCriteria.GetResult("secondCriteria"); IList firstResult = (IList)multiCriteria.GetResult("firstCriteria"); Assert.Greater(secondResult.Count, firstResult.Count); } RemoveAllItems(); }