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.  [ 5 posts ] 
Author Message
 Post subject: Criteria Query and Select Satement
PostPosted: Sun Sep 12, 2010 8:45 am 
Newbie

Joined: Wed Aug 25, 2010 12:52 pm
Posts: 12
Hi!

I'd like to know how to select just a few columns instead of all by using a criteria query.
Here is my current code:
Code:
        public ICollection<Geo> GetGeo(string city, int? plc, int? areaCode, string federalState)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(Geo));

                if (city.Length != 0)
                    criteria.Add(Expression.Sql("City LIKE ?", city + "%", NHibernateUtil.String));
                if (plc.HasValue)
                    criteria.Add(Expression.Eq("Plc", plc));
                if (areaCode.HasValue)
                    criteria.Add(Expression.Eq("AreaCode", areaCode));
                if (federalState != null)
                    criteria.Add(Expression.Eq("FederalState", federalState));

                return criteria.List<Geo>();
            }
        }

But up to now, I get all columns of the Geo table. How can I now select just city, plc, areaCode and federalState?

Regards, Joe


Top
 Profile  
 
 Post subject: Re: Criteria Query and Select Satement
PostPosted: Mon Sep 13, 2010 9:35 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look at projections:

http://nhforge.org/doc/nh/en/index.html#querycriteria-projection

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Criteria Query and Select Satement
PostPosted: Mon Sep 13, 2010 10:57 am 
Newbie

Joined: Wed Aug 25, 2010 12:52 pm
Posts: 12
Hi Wolfgang,

thanks for your help. I tried the following but I got a the following exception: Unable to perform find[SQL: SQL not available]
Do you know what I'm doing wrong?
Code:
                ICriteria criteria = session.CreateCriteria(typeof(Geo));

                if (plc.HasValue)
                    criteria.Add(Expression.Eq("Plc", plc));
                if (areaCode.HasValue)
                    criteria.Add(Expression.Eq("AreaCode", areaCode));
                if (federalState != null)
                    criteria.Add(Expression.Eq("FederalState", federalState));
                criteria.SetProjection(Projections.ProjectionList()
                    .Add(Projections.Property("City"), "City")
                    .Add(Projections.Property("Plc"), "Plc"));


Regards, Joe


Top
 Profile  
 
 Post subject: Re: Criteria Query and Select Satement
PostPosted: Mon Sep 13, 2010 11:01 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Is there an inner exception ? Can't see an obvious error. Try setting an alias in the criteria


ICriteria criteria = session.CreateCriteria(typeof(Geo), "g");

if (plc.HasValue)
criteria.Add(Expression.Eq("g.Plc", plc));
...

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Criteria Query and Select Satement
PostPosted: Mon Sep 13, 2010 11:41 am 
Newbie

Joined: Wed Aug 25, 2010 12:52 pm
Posts: 12
Thank you Wolfgang, you're right. There is no error!
The return value (I didnt't post) was the error. I got an instance of the class back.
Code:
return criteria.List<Geo>();


Regards, Joe


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