-->
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: joins in HQL
PostPosted: Wed Nov 26, 2003 10:46 am 
Newbie

Joined: Tue Sep 09, 2003 12:49 pm
Posts: 8
I have a table with versioned rows, and I want to retrieve the most recent row. The table structure is basically like this:
Code:
create table Price
(
   id int,
   amount numeric,
   effective_date datetime,
   product_id int
)

The current price for a product is the one with the most recent effective_date. I can get this in regular SQL via:
Code:
SELECT
   p.*
FROM
   price p
   LEFT JOIN price as p2 on p.product_id =  p2.product_id AND p.effective_date < p2.effective_date
WHERE
   p2.id is null

Price is mapped as a persistant object, and I'm trying to port the query to HQL. However, I'm not sure how to do the left join. It doesn't seem to let me specify the columns to join on, and I'm getting parse errors. What I tried is:
Code:
SELECT
   p
FROM
   Price as p
   LEFT JOIN Price as p2 on p.product.id = p2.product.id and p.effectiveDate < p2.effectiveDate
WHERE
   p2.id is null

Is this possible? Do I need to map Price as a many-to-many to itself on product_id in order to do the join? Any help would be appreciated.

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 11:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
HQL only allows left joins along associations. There is no ON clause. Use createSQLQuery()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 12:13 pm 
Newbie

Joined: Tue Sep 09, 2003 12:49 pm
Posts: 8
Thanks, that worked perfectly. That's definitely a cool feature!

Jon


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.