-->
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: Getting list of value objects
PostPosted: Tue Mar 20, 2007 12:23 pm 
Newbie

Joined: Thu Dec 21, 2006 4:34 am
Posts: 13
Is it possible to issue a hql query to directly return a list of value objects without also selecting the entity(s) that aggregates these value objects?

I know the above flys in the face of best practice but I'm in the middle of writing some test utilities where it would be useful in this context to instantiate some value objects without also loading their associated entities.

Thanks in advance

Christian Crowhurst


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 10:39 pm 
Beginner
Beginner

Joined: Sat Dec 10, 2005 6:22 pm
Posts: 28
Location: Chicago, IL
Check out this thread. You can create your own view objects and use projections and a result transformer to get the data back. Its fast! I just improved performance in a major way by migrating to 1.2 and updating some of my queries to use projections and view objects.

Adam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 5:08 pm 
Newbie

Joined: Thu Dec 21, 2006 4:34 am
Posts: 13
Thanks for the reply Adam.

Looks like I need to learn another area of NHibernate - Projections and result transformers.

I've just looked at chapter 12 and 13 of the NHibernate reference doco and see there's a little bit of coverage but not much. If you (or anyone else for that matter) have a spare moment, and your feeling in a generous mood, can you recommend any resource (maybe a link or two) to crib up on this subject?

Thanks
Christain


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 11:34 pm 
Beginner
Beginner

Joined: Sat Dec 10, 2005 6:22 pm
Posts: 28
Location: Chicago, IL
Your best bet might be looking at support for these things in regular java Hibernate. Usually I look for documentation there first since its a little more mature and there are more examples.

Projections are exactly what you would think, a SQL select clause. You can use them with criteria queries to bring only a certain number of columns back instead of hydrating the entire entity. You can also do aggregations: Count, Sum, Min, Max and Grouping.

I do not know much about result transformers, this tip came from an email. Here is the sample code I was given.

Code:
[Test]
public void QueryTestWithStrongTypeReturnValue()
{
       using (ISession session = OpenSession())
       {
              ICriteria c = session.CreateCriteria(typeof(ProjectionTestClass));

              NHibernate.Transform.IResultTransformer trans = new NHibernate.Transform.AliasToBeanConstructorResultTransformer(
                     typeof(ProjectionReport).GetConstructors()[0]
                     );
             
              c.SetProjection(Projections.ProjectionList()
                                   .Add(Projections.Avg("Pay"))
                                   .Add(Projections.Max("Pay"))
                                   .Add(Projections.Min("Pay")));
              c.SetResultTransformer(trans);
              ProjectionReport report = c.UniqueResult<ProjectionReport>();
              Assert.AreEqual(report.MinPay, 2.5);
              Assert.AreEqual(report.MaxPay, 4);
              Assert.AreEqual(report.AvgPay, 1);

       }
}


The above code from Ayende Rahien, his program, NHibernate Query Generator and Rhino-Commons are two that I actively use in my current projects.

When in doubt, look at the NHibernate source, its amazing what you may uncover.

Adam


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 22, 2007 5:54 pm 
Newbie

Joined: Thu Dec 21, 2006 4:34 am
Posts: 13
Thanks, I'll head on over to Hibernate resources if I get stuck. Doing a quick google I did find this link which gives a similar example to the one you provide: http://liangwu.wordpress.com/2007/03/13/create-dto-with-nhibernate/


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.