gavin wrote:
The second approach is correct, it is an SQL fragment, not a HQL fragment that is required.
What does the generated SQL look like?
The generated SQL looks like:
18:48:42,558 DEBUG [SessionFactoryImpl] prepared statement get: select customer0
_.variable_id as variable1___, customer0_.variable_id as variable_id, customer0_
.variable_value as variable2_, customer0_.version as version, customer0_.company
_variable_set_id as company_4_, customer0_.company_variable_set_group_id as comp
any_5_, customer0_.company_variable_id as company_6_, customer0_.customer_id as
customer7_ from customervariablevalue customer0_ where customer0_.customer_id=?
and customer0_.version = (select max(customer0_.version) from customervariablevalue) order by customer0_.version desc
I converted this to something more readable like:
select * from customervariablevalue customer_0 where customer_0.customer_id = 1 and
customer_0.version = (select max(customer_0.version) from customervariablevalue)
AND It DOES have a problem -- it returns all elements. But if you correct the table alias in the subquery to customer_0 and get this:
select * from customervariablevalue customer_0 where customer_0.customer_id = 1 and
customer_0.version = (select max(customer_0.version) from customervariablevalue customer_0)
THEN it works correctly and returns the max version. So, now I know what the problem is but how do I fix it? I do not want to guess an alias name in my mapping file. Is there a way to tell Hibernate not to use alias?
Thanks a lot
Alex