-->
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.  [ 4 posts ] 
Author Message
 Post subject: HQL query: left outer join on subquery
PostPosted: Tue Apr 18, 2006 2:49 pm 
Newbie

Joined: Tue Apr 18, 2006 1:44 pm
Posts: 2
Hello

My associations are listed below in the mapping file snippets.

I want to retrieve all games and if exists the tip for the game of a selected user. If the user hasn't made a tip for a game, null should be returned.
Among other queries I tried the following:
Code:
from Game g left outer join g.tips t where t.user = user and user = :user

The problem with the mentioned query is, that the where clause is applied to the whole query, not only to the tips collection. I got just the games back that the user has already tipped for instead of all available games.
My SQL query would look like the following:
Code:
select * from game g left join (select * from tip t where t.user_id = 'USER_ID') as user_tips on g.id = user_tips.game_id

How you can see it's just a left outer join on a subselect.
Am I able to achieve what I want with Hibernate? If so does anyone has a hint for me?

Thanks
Cheers Martin


Hibernate version:
3.1.3

Mapping documents:
tip:
Code:
      <many-to-one name="user" class="ch.erzberg.strafraum.User" column="user_id" not-null="true"/>
   
      <many-to-one name="game" lazy="false" class="ch.erzberg.strafraum.Game" column="game_id" not-null="true"/>


game:
Code:
        <!-- collection of tips -->
        <set name="tips" lazy="true" inverse="true" cascade="all-delete-orphan">
            <key column="game_id"/>
            <one-to-many class="ch.erzberg.strafraum.Tip"/>
        </set>

user:
Code:
        <!-- collection of tips -->
        <set name="tips" lazy="true" inverse="true" cascade="all-delete-orphan">
            <key column="user_id"/>
            <one-to-many class="ch.erzberg.strafraum.Tip"/>
        </set>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Postgresql 8.1

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

[list=][/list]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 12:34 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Does this work?
Code:
from Game g left outer join g.tips t where t.user is null or t.user = :user


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 4:31 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
try this
Code:
from Game g left outer join g.tips t with t.user = :user


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 12:08 pm 
Newbie

Joined: Tue Apr 18, 2006 1:44 pm
Posts: 2
Perfect. I tried both solution. They return exactly what a need. Thanks a lot.


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