-->
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: How to select specific properties using ICriteria?
PostPosted: Tue Sep 25, 2007 6:56 am 
Beginner
Beginner

Joined: Mon May 14, 2007 2:50 am
Posts: 22
Hibernate version:
1.2

I am searching for a way of how to select specific properties using ICriteria, and not all objects.

As an example, the expected end result is array of object[] , containing the required properties.
The code i expect might look like this:

Code:
IQuery sqlQuery = sess.CreateSQLQuery("select cat.Name, cat.Surname, ....  from cats cat");
sqlQuery.AddCritieria(Expression.Eq("Name", "kitty"))
sqlQuery.AddOrder(Order.Asc(Name"))
sqlQuery.SetMaxResults(50);
IList cats = sqlQuery.List();


What i actually found in NHibernate 1.2 is
* a SQL based query (http://www.hibernate.org/hib_docs/nhibe ... rysql.html).

Code:
sess.CreateSQLQuery("select {cat.*} from cats {cat}", "cat", typeof(Cat));


However, this sql query returns me a collection of cats, and not the properties i need.

* There are many more query types detailed in nhibernate chapter 9

but i could not find what i need, i.e. ICriteria fetching with ability to select distinct properties.


Is there a way to achieve this?



To summarize, what I want is ability to construct queries using ICriteria API, and to bypass hydration - instead of real objects i want to get a raw sql result table

Update
I found that projections might help. Reading them.. 12.7. Projections, aggregation and grouping


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 7:37 am 
Newbie

Joined: Mon Sep 24, 2007 4:22 pm
Posts: 6
Location: Ukraine, Kharkiv
You can use hql query instead of native query

select cat.Surname from Cat as cat where cat.Name = "kitty"

you will get a collection of strings


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 8:23 am 
Beginner
Beginner

Joined: Mon May 14, 2007 2:50 am
Posts: 22
tihobrazov wrote:
You can use hql query instead of native query

select cat.Surname from Cat as cat where cat.Name = "kitty"

you will get a collection of strings


That's feasible, but not what i need.

First - we have a structure which defines filters and visible columns for a grid, so constructing HQL would be very furstrating. strings is not what I am comfortable with

Second - i could not manage to write a HQL query with Limit support.



But for my problem i have found a solution, possibly.
Take a look at this:
ProjectionQuery on Ayende


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 9:39 am 
Beginner
Beginner

Joined: Mon May 14, 2007 2:50 am
Posts: 22
It seems i found solution.

As i said, it was about using projections.

The code which does what i want:

Code:
DetachedCriteria criteria = DetachedCriteria.For(typeof(Contact))
               .SetFirstResult(0)
               .SetMaxResults(1);
            
ProjectionList list = Projections.ProjectionList()
               .Add(Projections.Property("Name"))
               .Add(Projections.Property("Surname"));
ProjectionQuery<Contact> query = new ProjectionQuery<Contact>(criteria, list);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 8:36 pm 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
"To summarize, what I want is ability to construct queries using ICriteria API, and to bypass hydration - instead of real objects i want to get a raw sql result table"

Yeah for that you do want projection. But I'm wondering why you'd bother with NHibernate then? If you really, truly want to avoid all objects and want a table result-set, why not just use ADO.NET (or other database) directly?

I don't want to be cocky, but if you're going to ask how can I not use objects within Object software, then I suppose somebody has to ask this ;-)

You don't have to justify anything, but I'm interested in your reasons. Is there a known, worthwhile performance benefit for what you're doing? Do you know exactly what the benefit is (ie you've measured it).


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 5:46 am 
Beginner
Beginner

Joined: Mon May 14, 2007 2:50 am
Posts: 22
PandaWood wrote:
"
Yeah for that you do want projection. But I'm wondering why you'd bother with NHibernate then? If you really, truly want to avoid all objects and want a table result-set, why not just use ADO.NET (or other database) directly?
<...>
You don't have to justify anything, but I'm interested in your reasons. Is there a known, worthwhile performance benefit for what you're doing? Do you know exactly what the benefit is (ie you've measured it).


Your question hit the right spot.

I wrote about it my blog, here you can read it:
http://griuvesiai.blogspot.com/2007/09/performance-upgrade-month.html
http://griuvesiai.blogspot.com/2007/09/performance-upgrade-month-2.html


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.