These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: using Expression.Eq("b.class", typeof(XXX))
PostPosted: Sun Sep 02, 2007 5:02 pm 
Regular
Regular

Joined: Tue May 31, 2005 9:55 am
Posts: 67
I have a class hierarchy root at A with 2 children B, C

A
/ \
B C

I would like to do something like Property.ForName("b.class").Eq(typeof(A))
add be able to match against A B or C. However, it only does A. Is there another expression that will let me match the class against the entire hierachy?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 03, 2007 4:21 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
I'm looking for a similar thing.

See my thread Restrict criteria to subclass of associated object


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 07, 2007 3:49 pm 
Regular
Regular

Joined: Tue May 31, 2005 9:55 am
Posts: 67
I couldn't find anything either so I created my own criterion to do it.

Code:
   using System.Collections;
   using global::NHibernate;
   using global::NHibernate.Engine;
   using global::NHibernate.Expression;
   using global::NHibernate.Hql.Util;
   using global::NHibernate.Persister.Entity;
   using global::NHibernate.SqlCommand;
   using global::NHibernate.Type;

   public class IsAExpression : AbstractCriterion
   {
      private readonly Type _entityClass;
      private readonly string _propertyNameOrAlias;

      public IsAExpression(Type entityClass)
         : this(string.Empty, entityClass)
      {
      }

      public IsAExpression(string propertyNameOrAlias, Type entityClass)
      {
         _propertyNameOrAlias = propertyNameOrAlias;
         _entityClass = entityClass;
      }

      public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
                                            IDictionary enabledFilters)
      {
         string alias;
         Type targetType;
         ICriteria aliasedCriteria;

         if (string.IsNullOrEmpty(_propertyNameOrAlias))
         {
            alias = criteriaQuery.GetSQLAlias(criteria);
            targetType = criteriaQuery.GetEntityName(criteria);
         }
         else if ((aliasedCriteria = criteria.GetCriteriaByAlias(_propertyNameOrAlias)) != null)
         {
            alias = criteriaQuery.GetSQLAlias(aliasedCriteria);
            targetType = criteriaQuery.GetEntityName(aliasedCriteria);      
         }
         else
         {
            alias = criteriaQuery.GetSQLAlias(criteria, _propertyNameOrAlias);
            IType type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyNameOrAlias);

            if (!type.IsEntityType)
            {
               throw new QueryException("Only entities can be used with an IsAExpression");
            }

            targetType = type.ReturnedClass;
         }

         if (!targetType.IsAssignableFrom(_entityClass))
         {
            return new SqlString("1=0");   
         }

         IQueryable queryable = ObtainQueryable(criteriaQuery);
         SqlString condition = queryable.QueryWhereFragment(alias, true, true);

         if (condition.IndexOfCaseInsensitive(" and ") == 0)
         {
            condition = condition.Substring(5);
         }

         return (condition.Length > 0) ? condition : new SqlString("1=1");
      }

      public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
      {
         return EmptyTypes;
      }

      public override string ToString()
      {
         return _propertyNameOrAlias + " isa (" + _entityClass.FullName + ')';
      }

      public static IsAExpression Create(Type entityClass)
      {
         return new IsAExpression(entityClass);
      }

      public static IsAExpression Create<T>()
      {
         return new IsAExpression(typeof(T));
      }

      public static IsAExpression Create(string propertyNameOrAlias, Type entityClass)
      {
         return new IsAExpression(propertyNameOrAlias, entityClass);   
      }

      public static IsAExpression Create<T>(string propertyNameOrAlias)
      {
         return new IsAExpression(propertyNameOrAlias, typeof(T));
      }

      private IQueryable ObtainQueryable(ICriteriaQuery criteriaQuery)
      {
         IQueryable queryable = criteriaQuery.Factory.GetEntityPersister(_entityClass) as IQueryable;

         if (queryable == null)
         {
            queryable = SessionFactoryHelper.FindQueryableUsingImports(
               criteriaQuery.Factory, _entityClass.FullName);
         }

         return queryable;
      }

      private static readonly TypedValue[] EmptyTypes = new TypedValue[0];
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.