-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping hql query result to class
PostPosted: Tue Apr 18, 2006 5:24 pm 
Newbie

Joined: Tue Mar 21, 2006 4:18 pm
Posts: 11
Hello, I have a particular hql query, which returns to me certain read-only data, for example:
Code:
session.Find("select distinct e.Code, e.Text from Entity e where e.Type = 1")

or it could be some of group by query... So i cannot use Entity class for results. The result is array of arrays. I want these result to map to type-safe class. In documentation in section 11.4 i have found this:

Code:
Queries may return multiple objects and/or properties as an array of type object[]

select mother, offspr, mate.Name
from Eg.DomesticCat as mother
    inner join mother.Mate as mate
    left outer join mother.Kittens as offspr
or as an actual typesafe object

select new Family(mother, mate, offspr)
from Eg.DomesticCat as mother
    join mother.Mate as mate
    left join mother.Kittens as offspr
assuming that the class Family has an appropriate constructor.


My question: where this type-safe class should be defined to result nhibernate will find and use it? All my test ends with "NHibernate could not find the class....." error message during query execution.
This class need to be also mapped to database or not? Generally how to map particular query results to classes?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 5:57 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
http://forum.hibernate.org/viewtopic.php?t=955903&highlight=import


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 6:16 pm 
Newbie

Joined: Tue Mar 21, 2006 4:18 pm
Posts: 11
Thank you, i'll try it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 4:25 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We created a utility that takes an HQL statement in the first form,
Code:
select mother, offspr, mate.Name
from Eg.DomesticCat as mother
    inner join mother.Mate as mate
    left outer join mother.Kittens as offspr

and generates the C# code for the equivalent type-safe class. Actually, it generates a class that can be used as a type-safe class with a query of the second form,
Code:
select new Family(mother, mate, offspr)
from Eg.DomesticCat as mother
    join mother.Mate as mate
    left join mother.Kittens as offspr

but the generated class also contains a "map" of the the selected items and the entities that back them, as well as a version of the query without the SELECT clause. We call these generated classes "entity view row" types. We then have a univeral data access class that recognizes these generated "entity view row" types, executes the modified queries (sans SELECT clause), creates instances of the "entity view row" for each "row" returned by the query, and adds the entities in the "row" to a dictionary (keyed by alias defined in the query) in the "entity view row". A collection of these "entity view row" instances, which we call an "entity view", is returned by the data access class. The "entity view row"'s generated properties simply get and set the underlying property on the associated entity.

This allows us to have type-safe classes that are easily databound to controls (i.e. the query results are "flattened") and at the same time, the underlying entities that back each "entity view row" are available in full from the same result set. Best of all, we don't have to write these type-safe classes -- just type the HQL in the utility, and it spits out the generated code for the type-safe class. We're working on creating an HqlObjectDataSource component, which opens the HQL editor in Visual Studio and adds/updates the generated class in your project.

BTW, the dictionary of backing entity instances is marked as a non-browsable property, so when we databind our result set to controls (e.g. a DataGridView), all of the "selected" properties appear in the control by default, and none of the "overhead" properties.

Also, note that with this approach, every useful selected value in the "original" HQL query will be a primitive scalar property (string, integer, boolean, DateTime, etc). Each "entity view row" returned by the query will automatically have an instance of each entity in the FROM and JOIN clauses, so there's no point in actually selecting entities or entity properties in the "original" HQL specified to our utility.


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