-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problem with OR in query
PostPosted: Mon Apr 23, 2007 10:47 am 
Newbie

Joined: Mon Oct 18, 2004 12:29 pm
Posts: 8
Hello everyone, I have a very weird problem with a query. I'm using Hibernate 3.2.1 and mysql 5.0.37

The problem is that the query runs fine with just the first part of the WHERE clause, and fine with the second part of the WHERE clause also. If I put both together it doesn't give me any results

This works...
Code:
SELECT DISTINCT bus FROM MBus AS bus WHERE departureRegionRef.regionRef.name LIKE :regionName ORDER BY id DESC


This also works..
Code:
SELECT DISTINCT bus FROM MBus AS bus WHERE busAssignationRef.busRouteRef.departureRegionRef.regionRef.name LIKE :regionName1 ORDER BY id DESC


No go.. :-(
Code:
SELECT DISTINCT bus FROM MBus AS bus WHERE ((departureRegionRef.regionRef.name LIKE :regionName) OR (busAssignationRef.busRouteRef.departureRegionRef.regionRef.name LIKE :regionName1)) ORDER BY id DESC


It doesn't crash or anything... But it doesn't return the rows I'm expecting.

Thanx for the help...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 12:44 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Let's see the generated SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 2:56 pm 
Newbie

Joined: Mon Oct 18, 2004 12:29 pm
Posts: 8
Code:
Hibernate: select distinct mbus0_.id as id0_, mbus0_.system_entry as system2_0_, mbus0_.busy_entry as busy3_0_, mbus0_.status as status0_, mbus0_.bus_number as bus5_0_, mbus0_.bus_size as bus6_0_, mbus0_.bus_price_brutto as bus7_0_, mbus0_.initial_payment as initial8_0_, mbus0_.date_initial_payment as date9_0_, mbus0_.final_payment_done as final10_0_, mbus0_.date_final_payment as date11_0_, mbus0_.driver as driver0_, mbus0_.driver_handy as driver13_0_, mbus0_.new_bus as new14_0_, mbus0_.event_ref as event17_0_, mbus0_.event_location_ref as event15_0_, mbus0_.departure_region_ref as departure16_0_, mbus0_.bus_company_ref as bus18_0_, mbus0_.bus_owner as bus19_0_ from t_mbs_bus mbus0_, t_mbs_departure_region mdeparture1_, t_mbs_region mregion2_, t_mbs_bus_asignation mbusassign3_, t_mbs_bus_route mbusroute4_, t_mbs_departure_region mdeparture5_, t_mbs_region mregion6_ where mbus0_.departure_region_ref=mdeparture1_.id and mdeparture1_.region_ref=mregion2_.id and mbus0_.id=mbusassign3_.bus_id and mbusassign3_.bus_route_ref=mbusroute4_.id and mbusroute4_.departure_region_ref=mdeparture5_.id and mdeparture5_.region_ref=mregion6_.id and (mregion2_.name like ? or mregion6_.name like ?) order by mbus0_.id DESC


I don't know... from the gut I can tell that something's wrong ( on top of the fact that it's not working ;-) )... Any help is appreciated it...

Thanx again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 3:49 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Looks OK from a cursory glance, can you run that statement on your database directly and see if it returns any rows?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 4:57 pm 
Newbie

Joined: Mon Oct 18, 2004 12:29 pm
Posts: 8
It runs ok, but it doesn't return any rows. Based on the data it should. I remove the first part of the where clause in the hibernate query and it returns the expected rows.

Here's the sql query with that modified query:
Code:
Hibernate: select distinct mbus0_.id as id0_, mbus0_.system_entry as system2_0_, mbus0_.busy_entry as busy3_0_, mbus0_.status as status0_, mbus0_.bus_number as bus5_0_, mbus0_.bus_size as bus6_0_, mbus0_.bus_price_brutto as bus7_0_, mbus0_.initial_payment as initial8_0_, mbus0_.date_initial_payment as date9_0_, mbus0_.final_payment_done as final10_0_, mbus0_.date_final_payment as date11_0_, mbus0_.driver as driver0_, mbus0_.driver_handy as driver13_0_, mbus0_.new_bus as new14_0_, mbus0_.event_ref as event16_0_, mbus0_.event_location_ref as event18_0_, mbus0_.departure_region_ref as departure15_0_, mbus0_.bus_company_ref as bus19_0_, mbus0_.bus_owner as bus17_0_ from t_mbs_bus mbus0_, t_mbs_bus_asignation mbusassign1_, t_mbs_bus_route mbusroute2_, t_mbs_departure_region mdeparture3_, t_mbs_region mregion4_ where mbus0_.id=mbusassign1_.bus_id and mbusassign1_.bus_route_ref=mbusroute2_.id and mbusroute2_.departure_region_ref=mdeparture3_.id and mdeparture3_.region_ref=mregion4_.id and (mregion4_.name like ?) order by mbus0_.id DESC


:-(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 10:28 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
The distinct looks odd in there. What happens if you remove the DISTINCT?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 1:56 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Also, try using explicit joins instead of implicit ones. I'm thinking that one of your inner joins is not found, thus returning no rows.

e.g.
Code:
SELECT DISTINCT bus FROM MBus AS bus
  left join bus.departureRegionRef.regionRef as departure
  left join bus.busAssignationRef.busRouteRef.departureRegionRef.regoinRef as assignation
WHERE ((departure.name LIKE :regionName) OR (assignation.name LIKE :regionName1)) ORDER BY id DESC


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 2:30 pm 
Newbie

Joined: Fri Apr 20, 2007 3:46 pm
Posts: 15
I would agree with Anansi,

Ignore the actual or logic in the where clause. What you have most likely is that when joining the tables you specified some pk=fk relationship that do not exist and thus you have a null result set no matter what your OR clause says.

Try using left outer joing as anansi said.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 10:50 am 
Newbie

Joined: Mon Oct 18, 2004 12:29 pm
Posts: 8
Thanx for the suggestions... I was a bit sick yesterday so I couldn't get so much work done.

I need the Distinct clause... I'll try with the explicit joins..
Thanx again... I'll keep you guys posted

--- Edit ---

It seems to work out... I'm gonna check the data to be sure.. Thanx!


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