-->
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.  [ 6 posts ] 
Author Message
 Post subject: Check if alias exists
PostPosted: Thu Mar 27, 2008 6:04 pm 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
I have this query

Code:
ICriteria itemTranslationCriteria = NHibernateSession.CreateCriteria(typeof(Hairless.Entities.Translation.ItemTranslation), "it");
            ICriteria itemCriteria = itemTranslationCriteria.CreateAlias("Item", "i");
            itemCriteria.CreateAlias("i.Brand", "b");
            itemCriteria.CreateAlias("i.Stores", "s");
            itemCriteria.CreateAlias("i.SubCategory", "sub");

            if (filters != null)
            {
                foreach (ItemFilter filter in filters)
                {
                    if (filter is DepartmentFilter)
                    {
                        itemCriteria.CreateAlias("i.Departments", "d");
                    }

                    filter.Execute(itemCriteria);
                }
            }

            itemCriteria.CreateAlias("s.Currency", "cur") // Store filter. Default. Must be handled by developer.
               
            .CreateAlias("i.Images", "img")
            .Add(NHibernate.Expression.Expression.Eq("img.SortOrder", 0)) // Only get the first image. Not the best solution
           
            .SetProjection(Projections.ProjectionList()
            .Add(Projections.Property("i.Id"), "Id")
            .Add(Projections.Property("i.Price"), "Price")
            .Add(Projections.Property("i.MemberPrice"), "MemberPrice")
            .Add(Projections.Property("i.Junior"), "Junior")
            .Add(Projections.Property("i.Female"), "Female")
            .Add(Projections.Property("i.Male"), "Male")
            .Add(Projections.Property("it.Name"), "Name")
            .Add(Projections.Property("b.Name"), "BrandName")
            .Add(Projections.Property("img.File"), "Image")
            .Add(Projections.Property("c.Identifier"), "CollectionName") // We got this from Outlet filter. A default in every search
            .Add(Projections.Property("c.Discount"), "CollectionDiscount") // We got this from Outlet filter. A default in every search
            .Add(Projections.Property("i.Discount"), "ItemDiscount")
            .Add(Projections.Property("cur.Suffix"), "CurrencySuffix")
            .Add(Projections.Property("cur.Prefix"), "CurrencyPrefix"))
            .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Hairless.Communication.ItemBase)));
            // Execute Sort Filters if there is any
            // See ( Hairless.Entities.Sort namespace )
            if (sorts != null)
            {
                foreach (ItemSort sort in sorts)
                {
                    sort.Execute(itemCriteria);
                }
            }

            return itemCriteria;


In my foreach loop i iterate over 1-15 filter that limits the hits. My problem is that I need in my Filter classes to know what alias I have used.

Example. I have set an alias for Item to i. Now I need to know that I used alias i in my filter. Not very loosly copuled. And when I create an alias in my filter I must know that no alias has been made for the collection class.


Code:
public class OutletFilter : ItemFilter
    {
        private Boolean outlet;

        public OutletFilter(Boolean outlet)
        {
            this.outlet = outlet;
        }

        public void Execute(NHibernate.ICriteria criteria)
        {
            ICriteria collectionCriteria = criteria.CreateAlias("i.Collection", "c");

            if (outlet)
            {
                collectionCriteria.Add(NHibernate.Expression.Expression.Lt("c.StartDate", DateTime.Now));
                collectionCriteria.Add(NHibernate.Expression.Expression.Lt("c.EndDate", DateTime.Now));
            }
            else
            {
                collectionCriteria.Add(NHibernate.Expression.Expression.Le("c.StartDate", DateTime.Now));
                collectionCriteria.Add(NHibernate.Expression.Expression.Ge("c.EndDate", DateTime.Now));
            }

        }


If I have created an alias for the collection I'll get an hibernate exception.
Can this be done without using alias or can I check if an alias is made?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 4:15 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You have a property Alias on the criteria, wich shows the alias that is used for that criteria. Is that what you are looking for ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 6:37 am 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
I have tried that but it's not working.


Code:
if( criteria.Alias.Contains( '' ) )



Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 6:49 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You could keep track of the aliases you have created:

Code:
Dictionary<string,string> aliases = ...
aliases.Add("i.Collection", "c");


When do you get an exception ? When you create an alias that already exists ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 8:00 am 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
Yes, that is correct.

Here is my problem. When getting the Departments I must check
is there any departments in the current selection of item.

I make my item filter selection and set projection to department to get unique departments. I do the same for brand, color stories, categories etc. So I must use the same filters on every entity.

Saving it in a list is a good ide. Problem is that I want the filter class to be as loose as possible.

here is my filter interface

Code:
interface ItemFilter{
  void Execute( Criteria crit );
}


all filter classes implements this filter. I don't want to pass around a List in all the filters. Hope you know what I mean! Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 8:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not sure if this will work for you, but you could use subcriterias instead of the alias.

ICriteria subcriteria = itemCritera.CreateCriteria("Collection");

and pass the subcriteria to the filters. Then you don't have to worry about the alias. With that you loose the possibility to create an alias in the filter class, but speaking of loose coupling ....

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.