-->
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: left join within subselect
PostPosted: Fri Feb 24, 2006 6:11 am 
Newbie

Joined: Fri Nov 04, 2005 5:22 am
Posts: 2
Hibernate version: 3.1
Name and version of the database you are using: MySQL, Oracle

Hallo

I try to use a "left outer join" within a subselect but hibernate ignores the "left join" command and just generates an "inner join"! Is this a bug or is there something wrong with my code?

Any help is welcome
Thanks!
Sebastian.


This is a simplyfied version of my HQL-Query, please notice the "left outer join" between "Meeting" and "Deadlines" within the subselect.

Code:
select
   m
from
   Meeting m
where
   m.id in (
     select
       m.id
     from
       Meeting m left outer join m.deadlines d
     group by
       m.id
     having
       max(d.deadline) < :rightNow or
       max(d.deadline) = null


And this is the generated SQL. Please notice the "inner join" between "Meeting" and "Deadline" that should be an "left outer join".

Code:
select
  *
from
  HB_MEETING meeting0_
where
  meeting0_.ID in (
    select
      meeting1_.ID
    from
      HB_MEETING meeting1_, HB_DEADLINE deadlines2_
    group by
      meeting1_.ID
    having
     max(deadlines2_.DEADLINE)<'2006-02-24' or
     max(deadlines2_.DEADLINE) is null
)



Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 26, 2006 5:43 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Are you reading that right? There's no inner join in the generated SQL. There's no join at all, just a cross select. This performs like a full outer join. It's still not what I'd have thougth your HQL should produce, but it will contain all the rows you're looking for.

If there was an innter join, you'd see something like
Code:
    from
      HB_MEETING meeting1_
      inner join HB_DEADLINE deadlines2_ on meeting1_.ID = deadlines2_.MEETING_ID


Top
 Profile  
 
 Post subject: re
PostPosted: Mon Feb 27, 2006 6:04 am 
Newbie

Joined: Fri Nov 04, 2005 5:22 am
Posts: 2
Thanks for your reply,

Your right there is no join just a cross select in the example. I thought that hibernate generates a join when i join a object with his collection like "meeting" and "deadline" because the necessary where part of the join clause is whitin the hibernate mapping.
Sorry for this. However even if i modify the example like this:

Code:
select
   m
from
   Meeting m
where
   m.id in (
     select
       m.id
     from
       Meeting m
         left outer join
       m.deadlines d
     where
       m.id = d.meeting.id
     group by
       m.id
     having
       max(d.deadline) < :rightNow or
       max(d.deadline) = null
  )


hibernates generates somthing like this:

Code:
select
  *
from
  HB_MEETING meeting0_
where
   meeting0_.ID in (
      select
        meeting1_.ID
      from
        HB_MEETING meeting1_, HB_DEADLINE deadlines2_
      where
        meeting1_.ID=deadlines2_.MEETING_ID
      group by
        meeting1_.ID
      having
        max(deadlines2_.DEADLINE)<? or
        max(deadlines2_.DEADLINE) is null
     )
  )


and thats not what i want. I need a "left join" whitin the subselect not an "inner join" because in this way all meetings that have no deadlines will not be part of the result.

When i excecute the subselect not as subselect but just as a select like:

Code:
select
  m.id           
from
  Meeting m
     left outer join
   m.deadlines d
where
   m.id = d.meeting.id
group by
   m.id
having
   max(d.deadline) < :rightNow or
   max(d.deadline) = null



hibernates generates:

Code:
select
  meeting0_.ID as col_0_0_
from
  HB_MEETING meeting0_
    left outer join
  HB_DEADLINE deadlines1_
    on
  meeting0_.ID=deadlines1_.MEETING_ID
where
  meeting0_.ID=deadlines1_.MEETING_ID
group by
  meeting0_.ID
having
  max(deadlines1_.DEADLINE)<? or
  max(deadlines1_.DEADLINE) is null


Now i got my "left outer join" and by the ways it seems like its needless to specify a where clause like "meeting0_.ID=deadlines1_.MEETING_ID" in hql.

So where is my mistake? Wy depends the generated SQL of the position of a select. Should not be the generated SQL always be the same regardless if its a subselect or not?


Thanks for your help.

Regards Sebastian.


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.