/// <summary> /// Sets the fetch mode. /// </summary> /// <typeparam name="T">Type setting fetch mode for.</typeparam> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="criteria">The criteria.</param> /// <param name="associationPropertyExpression">The association property expression.</param> /// <param name="mode">The mode to set.</param> /// <returns>A <see cref="ICriteria"/></returns> public static ICriteria SetFetchMode <T, TProperty> ( this ICriteria criteria, Expression <Func <T, TProperty> > associationPropertyExpression, FetchMode mode) { var associationPath = PropertyHelper.ExtractPropertyName(associationPropertyExpression); criteria.Fetch(associationPath); return(criteria); }
/// <summary> /// Add the Fetch clauses to the query according to the given expand paths, using the ICriteria API /// </summary> /// <param name="criteria">The query to expand</param> /// <param name="expandPaths">The names of the properties to expand. May include nested paths of the form "Property/SubProperty"</param> /// <param name="sessionFactory">Provides the NHibernate metadata for the classes</param> /// <param name="expandMap">If provided, will be populated with the names of the expanded properties for each type.</param> /// <returns></returns> public static ICriteria ApplyExpansions(ICriteria criteria, string[] expandPaths, ISessionFactory sessionFactory, IDictionary <Type, List <string> > expandMap = null) { if (criteria == null) { throw new ArgumentException("Criteria cannot be null"); } if (!expandPaths.Any()) { throw new ArgumentException("Expansion Paths cannot be null"); } foreach (var expand in expandPaths) { // We always start with the resulting element type var currentType = criteria.GetRootEntityTypeIfAvailable(); var dotpath = expand.Replace('/', '.'); criteria = criteria.Fetch(SelectMode.JoinOnly, dotpath); // Add the types and properties to the expandMap so they will be serialized foreach (var seg in expand.Split('/')) { if (expandMap != null && !expandMap.ContainsKey(currentType)) { expandMap.Add(currentType, new List <string>()); } var metadata = sessionFactory.GetClassMetadata(currentType); if (metadata == null) { throw new ArgumentException("Type '" + currentType + "' not recognized as a valid type for this Context"); } // Gather information about the property var propInfo = currentType.GetProperty(seg); if (propInfo == null) { throw new ArgumentException("Type '" + currentType.Name + "' does not have property '" + seg + "'"); } if (expandMap != null) { expandMap[currentType].Add(seg); } var propType = propInfo.PropertyType; currentType = propType; } } return(criteria); }
private void PopulateChildrenCriteria(EntityNodeInfo nodeInfo, ICriteria rootCriteria, ICriteria parentCriteria, ref bool rootHasList, List <ICriteria> childCriterias) { if (nodeInfo == null) { return; } if (nodeInfo.Level > 0) //it's not the root node { if (nodeInfo.IsList) { if (rootHasList) { if (nodeInfo.Level == 1) //create new criteria { parentCriteria = rootCriteria.CreateCriteria(nodeInfo.PathName, JoinType.LeftOuterJoin); childCriterias.Add(parentCriteria); } else //batch fetch should be faster { return; } } else { parentCriteria.Fetch(SelectMode.Fetch, nodeInfo.PathName); rootHasList = true; } } else { parentCriteria.Fetch(SelectMode.Fetch, nodeInfo.PathName); } } foreach (var childInfo in nodeInfo.Children) { PopulateChildrenCriteria(childInfo, rootCriteria, parentCriteria, ref rootHasList, childCriterias); } }
public void AllowSingleCallSyntax() { ICriteria expected = CreateTestCriteria(typeof(Person)); expected.Add(Restrictions.IsNotEmpty("Children")); expected.AddOrder(Order.Asc("Name")); expected.Fetch("PersonList"); expected.SetLockMode(LockMode.UpgradeNoWait); IQueryOver <Person, Person> actual = CreateTestQueryOver <Person>(); actual.WhereRestrictionOn(p => p.Children).IsNotEmpty(); actual.OrderBy(p => p.Name).Asc(); actual.Fetch(SelectMode.Fetch, p => p.PersonList); actual.Lock().UpgradeNoWait(); AssertCriteriaAreEqual(expected, actual); }
public static ICriteria FetchAllProperties <TEntity>(this ICriteria criteria, ISessionFactory sessionFactory) { var entityType = typeof(TEntity); var metaData = sessionFactory.GetClassMetadata(entityType); for (var i = 0; i < metaData.PropertyNames.Length; i++) { var propType = metaData.PropertyTypes[i]; // get eagerly mapped associations to other entities if (propType.IsAssociationType && !metaData.PropertyLaziness[i]) { //criteria = criteria.SetFetchMode(propType.Name, FetchMode.Eager); criteria = criteria.Fetch(SelectMode.JoinOnly, propType.Name); } } return(criteria); }