-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problem with select and null fields
PostPosted: Wed Mar 03, 2010 1:05 pm 
Newbie

Joined: Wed Mar 03, 2010 12:53 pm
Posts: 4
Hello,
i'm using JPA with hibernate and i have a problem in a query with null fields. The problem is that in my results not happear tuples with fields with null values.

My problem is like this:
Person is a triple (Car car, Boat boat, dog dog)

String query="select new util.PersonReport(p.id, p.car.model, p.boat.speed, p.dog.color) from Person p";

the problem is that car, boat and dog are nullable and when one of this is null the intere tuple not happear in my results.
I want ask how i can avoid this problem.

Thanks
RM


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Wed Mar 03, 2010 2:52 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need to explicitly do a "left join" to the car, boat and dog properties. Implicit joins (dot notation) use "inner join" which excludes null references. So, your query should be something like:

Code:
select new util.PersonReport(p.id, c.model, b.speed, d.color) from Person p
left join p.car c left join p.boat b left join p.dog d


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Thu Mar 04, 2010 6:33 am 
Newbie

Joined: Wed Mar 03, 2010 12:53 pm
Posts: 4
Thanks
very clear and useful!

It run!


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Fri Mar 19, 2010 12:33 pm 
Newbie

Joined: Wed Mar 03, 2010 12:53 pm
Posts: 4
Hello,
Can you tell me how i can create a join in the case in which i have a N-to-1 relationship?
For example Car-Person is a Nto1 relationship.
How i can select all the cars of my database showing also the cars that don't have owner (person)? I have to fill a Bean like this:

String query="select new util.MyCar(c.id, c.model, p.name, p.surname) from Car c, Person p ??? ";

??? -> what kind of join can i use?

Please help me i have to solve this problem, i know that exsist a solution...
Thanks
RM


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Fri Mar 19, 2010 3:00 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It's the same thing... assuming that your Car has an "owner" attribute that is a Person.

Code:
select new util.MyCar(c.id, c.model, p.name, p.surname) from Car c left join c.owner p


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Mon Mar 22, 2010 1:35 pm 
Newbie

Joined: Mon Mar 22, 2010 1:23 pm
Posts: 3
Hi to all,

I have the same problem of rmarcello and I try to apply the nordborg's solution but it dosen't work.
I have two tables:

Person (person_id, name, surname) and Car (car_id, model, colour, owner)

where owner in Car table is a FK to person_id field of Person table.
Person and Car are linked with a N-to-1 relationship (a person can have zero or more cars and a car is owned only by a person).

I try to execeute this HQL query:
Code:
select p.personId, p.name, c.model, c.colour
from Person p
left join p.car.owner


but I get a Null Pointer Exception.

What's wrong in my query?
Thanks to all that can help me.


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Mon Mar 22, 2010 2:28 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
What's wrong in my query?


You have not defined the 'c' alias, though I don't know if it is related to the NullPointerException or not.

Code:
select p.personId, p.name, c.model, c.colour
from Person p
left join p.car c


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Tue Mar 23, 2010 1:08 pm 
Newbie

Joined: Mon Mar 22, 2010 1:23 pm
Posts: 3
Sorry nordborg I've a mistake in my query.

My previous query was:
Code:
select p.personId, p.name, c.model, c.colour
from Person p
left join p.car.owner


but I can not use the field p.car beacuse from person table there is no way to reach car table.
The unique way to get this information is to start my query from car table but I've to use a right join beacuse I want keep
all people's info even if they have no car. I tried to execute this query
Code:
select p.personIs, p.name, c.model, c.colour
from Car c
right join c.owner p


but the obtained results are not exact because I can not see the information about people that have no cars.
Finally the questions are:

1) Can you use the right join starting from car table, and what is the correct manner to do this?
2) Can I start my query from table person? (Hibernate have created an HashSet of car)

Thank you very much!


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Tue Mar 23, 2010 5:10 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
In this stage it would really help if you posted your mappings or code+annotations. It is hard to guess what you have defined and what you have not defined. You can also try to enable logging of the SQL that Hibernate generates for your query and see if that makes sense or not. How to enable logging is described here: http://docs.jboss.org/hibernate/stable/ ... on-logging

Quote:
I can not use the field p.car beacuse from person table there is no way to reach car table.


Strange... in your very first post you are using p.car.model and when I showed you how to use left join you answered that it worked.


Top
 Profile  
 
 Post subject: Re: Problem with select and null fields
PostPosted: Thu Mar 25, 2010 4:47 am 
Newbie

Joined: Mon Mar 22, 2010 1:23 pm
Posts: 3
Nordborg the right join solution works perfectly! I had a stupid mistake in the select field and the obtained results was not appear like as expected. I want to thank you for your meaningful support.


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