-->
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.  [ 3 posts ] 
Author Message
 Post subject: Can I map an aggregate function as a class field?
PostPosted: Sun Feb 03, 2008 1:03 pm 
Newbie

Joined: Tue Nov 20, 2007 12:03 pm
Posts: 13
Hi guys,

I have two tables, teams and workers with a many to one association between them (one team --> many workers).
I have two classes Respectively, Team and Worker, where Team has a collection of Workers.

My client wants two way access to the data:
1) Get teams with each team's workers details [select * from workers...].
2) Get teams with only number of workers [select count(*) from workers where team_id = ..."

This is easily done with sql but I don't know how to do this in NHibernate.
I could count the Workers in the collection but it's lazy loaded and should remain like this due to the amount of data (it's not efficient to do it like this anyway)

basically I want to add a NumOfWorkers field to my Team class that its value is the result of "select count * from..".
This should be automatically executed when NHibernate retrieves the Team from the DB.

How can I accomplish this? Is there a mapping technique that I can use or an HQL/SQL to add somewhere?

Thanks in advance,
Shaul


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 03, 2008 2:23 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Define a formula property, where you can put SQL code.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Sun Feb 03, 2008 4:18 pm 
Newbie

Joined: Tue Nov 20, 2007 12:03 pm
Posts: 13
Thanks!
I Tried it and it worked.
Shaul

gonzao_diaz wrote:
Define a formula property, where you can put SQL code.


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