-->
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: Query on multiple table, one might be empty
PostPosted: Fri Jul 15, 2005 11:26 am 
Newbie

Joined: Mon Jun 27, 2005 10:29 am
Posts: 4
Hibernate version:
2

Name and version of the database you are using:
Oracle 9i

Let's say I have a pojo called Person, wich contains the fields name, age and id. I also have a pojo called Races, that contains the fields date, raceNumber racer (instance of Person) and id.

If a person has raced 3 times, my database will have in the race table 3 races, with their dates and raceNumber from 1 to 3 and the same racer.

Now, suppose I'd like to retrieve a racer from my database along with the date of his first race using the racer's name. So I created a pojo called Info which contains name, age and date.
So my query would end up lookin like this

Code:
String query = "select new com.mySystem.pojo.Info(
                          race.date,
                          person.name,
                          person.age
                       from
                          Race race,
                          Person person
                      where
                          race.raceNumber = 1
                          and race.racer.id = person.id
                          and race.name = supplied_name";


I now there might be errors in this query but it is only so you can understand what I am trying to do. Now the real problem is this:
What if the person has never raced before? I still want his info, but with a query like this I will get nothing, since the results depend on the existence of entries in the race table. How can I fix this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 11:32 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
You need to do an outer join. You're currently doing an inner join (implicitly). There are plenty of SQL tutorials on the web that can explain how these work. The Hibernate documentation explains how to use HQL to accomplish this as well.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 7:20 am 
Newbie

Joined: Mon Jun 27, 2005 10:29 am
Posts: 4
Thank you, I will look for them.


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.