views:

298

answers:

2

I have a database that is being used as a sort of version control system. That is, instead of ever updating any rows, I add a new row with the same information. Each row also contains a version column that is a date timestamp, so the only difference is the new row will have a more recent timestamp.

What I'm having trouble with is writing an efficient hibernate query to return the latest version of these rows. For the sake of example, these are rows in a table called Product, the timestamped column is version. There are multiple versions of multiple products in the table. So there may be multiple versions (rows) of ProductA, multiple versions of ProductB, etc. And I would like to grab the latest version of each.

Can I do this in just a single hibernate query?

session.createQuery("select product from Product product where...?");

Or would this require some intermediate steps?

A: 

To find the latest version of a particular product:

select product where ... order by version DESC

put a compound key with the version too.

To find the latest version of all products, you need two queries (or a subquery):

Insert into TMP (id, version) select (id, max(version)) from product group by id);
select P.* Product P, TMP where TMP.ID = P.ID
Justin
I think the OP want's the latest version of all products, this gives all versions ordered newest first. E.g. if there were 5 copies of product A, and one of B, and all of A were modified more recently than B, then you would get 5 instances of A followed by B. I think the OP wants just one instance of A and one row of B - the latest one in both cases.
mdma
If he is querying for just one product, run the query and select the first row.
Justin
Sure, but that's not the question being asked. Quote: "And I would like to grab the latest version of each."
mdma
+1  A: 

To answer this, each product needs some kind of identifier. Since there can be multiple versions, we need to know "what is a product" I'm assuming product.id is out, as this is probably a surrogate key. I'll choose product.name for sake of example.

Here's a single query to retrieve the latest version of each product:

select p1 from Product p1 where
  p1.timestamp >= all (
     select p2.timestamp from Product p2 where
      p2.name=p1.name
  )

(My HQL is a bit rusty, but I hope you get the gist.)

The trick is the self join between like products of differing timestamps. p1 products are more recently modified than p2. To find the most recent p1, we find rows where there are no p2 values - i.e. there are no product more recent than p1.

EDIT: I've revised the query - I'd saw someone used the "on" syntax in a forum post recently, but I now remember that that is not valid HQL. Sorry about that - I don't have a system available to test on. It now uses a correlated subquery that functions the same as the JOIN.

Hope this helps.

mdma
This makes a lot of sense, thank you for the explanation. Unfortunately I'm having a hell of a time converting this into usable HQL. I'm generally opposed to asking for the answer to be directly handed to me, but writing queries is definitely not my area. If anybody could help clear this up I would sincerely appreciate it.
Slim
Was about to comment the previous answer to say that you need an association in HQL for out join. But the update is correct, this new query works.
Pascal Thivent
mdma - thank you so much for both a wonderful answer and a username that's just as great ;)Pascal Thivent, thanks for the confirmation.This has been an incredibly helpful answer all around. Cheers!
Slim
Hehe! You're welcome. The username comes from my days at uni - it was given to me by the sysadmins, but I guess they weren't aware of what they were dishng out ;-)
mdma