-->
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.  [ 2 posts ] 
Author Message
 Post subject: Aggregating joined data in select
PostPosted: Tue Oct 26, 2010 6:18 pm 
Newbie

Joined: Thu Sep 30, 2010 4:06 pm
Posts: 5
Hello,

I would like to map/fetch a max date from the many side of a 1:m relationship. So when I fetched a Customer, for instance, the MAX order date for all Orders related to the Customer would be populated in a MaxOrderDate field of that Customer object.

The SQL would look like this:

Code:
select c.CustomerName, c.CustomerPhoneNumber, (select max(DateOrdered) from Orders where CustomerID = c.ID) as MaxOrderDate from Customers c


Does anyone know of a way to do this without writing the HQL or SQL?

Many thanks!


Top
 Profile  
 
 Post subject: Re: Aggregating joined data in select
PostPosted: Tue Oct 26, 2010 7:32 pm 
Newbie

Joined: Thu Sep 30, 2010 4:06 pm
Posts: 5
I was able to do this in the declarative mapping using <property name="MaxOrderDate" formula="(select max(OrderDate) from Orders o where o.CustomerID=CustomerID)" ...

However, this requires hard-coding the db schema into the mapping which is hard to maintain. Does anyone know of another way to do it?


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