-->
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: Query on period-based many-to-many relation
PostPosted: Mon Nov 09, 2009 9:08 pm 
Newbie

Joined: Mon Nov 09, 2009 8:20 pm
Posts: 1
Suppose there are two entities, Developer and Team. A Developer works on a Team for some time and then switches to another Team. So there is also an entity DeveloperTeam that has dateStart and dateEnd attributes (dateEnd may be null).

I would like to fetch all Developers from the database along with their current Teams.

Example:
Code:
> select * from DeveloperTeam;
id  | developer |      datestart      |       dateend       |  role  | team
----+-----------+---------------------+---------------------+--------+------
  1 |         1 | 2009-02-04 00:00:00 | 2009-04-01 00:00:00 | ROLE_A |    1
  2 |         1 | 2009-04-01 00:00:00 |                     | ROLE_B |    2
  3 |         2 | 2008-02-06 00:00:00 | 2009-01-01 00:00:00 | ROLE_B |    5
  4 |         2 | 2009-01-01 00:00:00 |                     | ROLE_C |    3

Expected query result would be a set of two DeveloperTeam objects, id=2 and id=4, plus one DeveloperTeam object with id=null for each Developer who has not been assigned to any Team.

How can this be done? I've tried building a query that selects max(dateStart) and groups the results by developer, but weirdly that returned a set of Object instead of DeveloperTeam.

Thanks in advance for any suggestions!


Top
 Profile  
 
 Post subject: Re: Query on period-based many-to-many relation
PostPosted: Tue Nov 10, 2009 6:51 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Does it really have to be one single query?

In two queries it's easy:
Code:
select t from DeveloperTeam t where t.dateEnd is null

Code:
select d from Developer d where d is not in (select t.d from DeveloperTeam t where t.dateEnd is null)


If you want to use a single query or you really need DeveloperTeam-objects instead of developers in the second query you will need a constructorwithin your select.

_________________
-----------------
Need advanced help? http://www.viada.eu


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.