-->
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.  [ 2 posts ] 
Author Message
 Post subject: Grouping an joining
PostPosted: Mon May 19, 2008 8:31 am 
Newbie

Joined: Mon May 19, 2008 7:45 am
Posts: 4
Hi,

I have the following situation.
I have an entity Employee that consists of the attributes

-id (Primary key)
-loginname
-version
-email
-givenName
-lastName
-organizationalUnit

loginname and version are the natural identifier.

Everytime a user logs on to the application a LDAP query checks if the attributes are still correct. If one of the attibutes email, givenName,
lastName, organizationalUnit has changed the Employee will be versioned.

Now my problem:
I want to create a HQL query that gives me a collection of all Employees with their last version.
This is my SQL query that returns the IDs of the Employee objects:

Code:
SELECT
    e2.id
FROM
   (SELECT loginname, max(version) AS version
    FROM Employee
    GROUP BY loginname) e1
   
    INNER JOIN
   
   (SELECT loginname, version, id
    FROM Employee) e2 ON e1.loginname= e2.loginname AND e1.version= e2.version


Now I want to use these IDs in an IN-clause of an HQL query that returns a list of objects of type Employee.

The base HQL query for the Employee list looks like:
Code:
FROM Employee e WHERE e.id IN (...)


Finally I have to merge the first query into the IN-condition of the second query. That's what's not working for me.

Any ideas?

Thank you in advance,
Thilo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 8:39 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
How about
Code:
from Employee e1 where e1.version = (select max(e2.version) from Employee e2 where e1.loginName = e2.loginName)

_________________
-----------------
Need advanced help? http://www.viada.eu


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