-->
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: simple left outer join query
PostPosted: Wed Sep 07, 2005 1:19 pm 
Newbie

Joined: Tue Feb 10, 2004 9:26 am
Posts: 12
Hibernate version:
3.0.5

Mapping documents:
The table mappings are:
<hibernate-mapping>
<class name="com.test.item" table="item">
<id name="itemId" column="itemid" type="long">
<generator class="sequence">
<param name="sequence">item_itemid_seq</param>
</generator>
</id>
<property name="name" type="string" not-null="true"/>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="com.test.itemOpt" table="itemopt">
<composite-id>
<key-many-to-one name="item" column="itemid"/>
<key-many-to-one name="opt" column="optid"/>
</composite-id>
</class>
</hibernate-mapping>

I had a look and I couldn't find any clear, step by step documentation on how to do a normal simple left outer join query.

I have an item table and an itemOpt table.
I would like to do a query that returns all item names and either the itemOpt record or null.

I can do this in sql using:
SELECT item.name, itemOpt
FROM item LEFT OUTER JOIN itemOpt ON (item.itemId = itemOpt.itemId);

But I have totally butchered my project trying to get this to work in hibernate/HSQL and 8 hours later I am writing this post.

I have getters setters in 'com.test.item' for:
itemId
name

I have getters setters in 'com.test.itemOpt' for:
item
opt

Any help would be greatly appreciated.
I am sure it is a simple thing and maybe I'm just tired but it seems very difficult to find information on it.

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 3:52 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Code:
select item.name, itemOpt

from Item as item

left join ItemOpt as itemOpt


And you need to have the relation between item and itemOpt inside you <one-to-many> and probable <many-to-one> in your XML mapping.


Regards.

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 4:57 am 
Newbie

Joined: Tue Feb 10, 2004 9:26 am
Posts: 12
Thanks for the prompt reply. :D
Sorry If I'm being a bit thick but I want to make sure I understand what I have to do before I waste more time.

So the itemOpt file is fine as it has the correct getters and setters in the domain class and has a '<key-many-to-one' setup in the mapping file.

So I am assuming that my problem is I need to setup this nullable relationship for the item table.
So would I need to add a <one-to-many> relationship setup in the mapping file for item and also add a getter and setters in the item's domain object for (com.test.itemOpt)itemOpt ?

This would make the item table mapping look like this:
<hibernate-mapping>
<class name="com.test.item" table="item">
<id name="itemId" column="itemid" type="long">
<generator class="sequence">
<param name="sequence">item_itemid_seq</param>
</generator>
</id>
<property name="name" type="string" not-null="true"/>

<one-to-many name="itemOpt" column="itemoptid" not-null="false"/>
</class>
</hibernate-mapping>


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.