I think I've got it. The SQL query comes out as this shape:
Code:
select t.* from Table t
where t.version = (select max(m.version)
from Table m
where m.objid = t.objid)
I'm using "objid" as the group-by column, because I don't know what your various column names are. If there are two versions of object "obj", they'll both have the same objid, but different versionids (and optionally, rowids). If you stick indices on version and objid columns, this query should be fairly efficient.
Anyway, that SQL did what I expected it to do, and I hope it's what you want. I converted it to criteria, and this is the resulting code:
Code:
DetachedCriteria dc = DetachedCriteria.forClass(YourClass.class, "subq");
dc.setProjection(Property.forName("VERSION_ID").max());
dc.add(Property.forName("subq.OBJ_ID").eqProperty("qry.OBJ_ID"));
Criteria crit = session.createCriteria(YourClass.class, "qry");
crit.add(Property.forName("VERSION_ID").eq(dc));
return crit.list();
I haven't compiled that code, but once you work out any typos, etc., hopefully it'll do what you need.