-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to write a simple outer Join HQL?
PostPosted: Thu Sep 01, 2005 9:42 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi,

I have check the doc and I dont see any way to do this simple HQL. I have 2 table and I want to do a join between then based on 2 keys. This seems quite trivial, I and dont see anyway to do this:


Code:
select cashMovement, rrifLifDetail

from CashMovement cashMovement

left outer join RrifLifDetail rrifLifDetail

on rrifLifDetail.cashMvtRequestUid = cashMovement.requestUid


This HQL statement should sound like that :

"I want all cashMovement and all rrdfLifDetail where rrifLifDetail.cashMvtRequestUid = cashMovement.requestUid. And because it is an Left Outer join, give me a NULL rrifLifDetail if you find no rrifLifDetail according to rrifLifDetail.cashMvtRequestUid = cashMovement.requestUid."

How can I write this Statement? Must I use Native SQL to do this?

Thanks.

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 11:14 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Resulting HQL may be very different depend on your mapping. For instance, is these two entity have a link in mapping or not?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 11:15 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
Not absolutely sure about this, but you should specify what 'rrifLifDetail' means in the objects relation...

It should work with something like:

select cashMovement.something, rrifLifDetail.something

from CashMovement cashMovement

left outer join cashMovement.rrifLifDetail rrifLifDetail

where rrifLifDetail.cashMvtRequestUid = cashMovement.requestUid


If you are about to retrieve entire objects, you should forget about select, something like this:

from CashMovement cashMovement

left outer join cashMovement.rrifLifDetail rrifLifDetail

where rrifLifDetail.cashMvtRequestUid = cashMovement.requestUid

Hope it helps.
Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 11:30 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Thanks both,

I didn't linked in the xml mapping CashMovement to RrifLifDetail (sorry for that name) in a one-to-many relationship. I could do this, but it would not help me in the future. I need to be able to do some HQL join (and left join) between unlinked tables. Per exemple, I work with 2 databases (same database server) with 2 differents domain models (for each database), and linking everything between the 2 domain model would be too verbose.

To explain a bit the relationship this is how the 2 table are linked:
cashMovement.requestUid is the primary key of cashMovement and
rrifLifDetail.cashMvtRequestUid is a foreign key pointing to cashMovement.requestUid.

The orginal SQL look like this (the statement was simplified, removing the where clause, other left join and the order by...) :


Code:
SELECT DISTINCT  CASHMOVEMENT.Request_UID,
  CASHMOVEMENT.FromAccount,
  RRIFLIFDETAIL.FkAccount_ID,
  CASHMOVEMENT.SettlementDate,
  CASHMOVEMENT.PaymentMethod,
  CASHMOVEMENT.Amount,
  RRIFLIFDETAIL.FedTaxAmount,
  RRIFLIFDETAIL.ProvTaxAmount FROM  CashMovement CASHMOVEMENT
 
  LEFT JOIN  RrifLifDetail  RRIFLIFDETAIL ON  RRIFLIFDETAIL.CashMvt_Request_UID= CASHMOVEMENT.Request_UID
 




Thanks for helping!

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 7:12 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
etienne wrote:
Thanks both,

I didn't linked in the xml mapping CashMovement to RrifLifDetail (sorry for that name) in a one-to-many relationship.


Hibernate doesn't support outer joins on dynamic associations (not presented in mapping). The only option - native queries.

etienne wrote:
I could do this, but it would not help me in the future.


Quite the contrary, actially. Hibernate can't do many important things without presence of this information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 9:39 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
To be able to accomplish left outer join between 2 table, does anyone know if it is possible to include the links inside the mapping without adding the propreties in the pojo? I work with a very big and complexe legacy system on AS400/DB2, I would prefere to minimize the size of the pojos by not adding all relations (class one-to-one, set one-to-many, class many-to-one) in the pojo.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 9:54 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Look at Hibernate 3.1 - it is introduced new 'with' keyword:

Quote:
You may supply extra join conditions using the HQL with keyword.

from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight > 10.0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 10:05 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Thanks a lot! This will help me!

I'll work on that.

Regards,

Etienne.

(no more credit to give you, but thanks anyway!)


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