Hello, we have working HQL query to display list of users and last date they passed any Course:
Code:
select u, max(r.finish) from User u left join u.results r group by u order by u.username desc
It returns list of rows: arrays each containing two objects: User and Date.
The following criteria query also works ok
Code:
List listResult =
session.createCriteria(User.class)
.createAlias("results", "r", Criteria.LEFT_JOIN)
.setProjection(
Projections.projectionList()
.add(Projections.groupProperty("username"))
.add(Projections.max("r.finish"), "maxFinish")
.addOrder(Order.desc("maxFinish"))
.list();
It returns list of rows: arrays each containing two objects: String (username) and Date.
Is there a way to specify in Criteria query that entire User entity will be returned instead of just any User property, like in HQL query?
Thank you in advance for your help.
Hibernate version: We're using 3.1.2 hibernate version
Mapping documents:There are two entity classes in the one-to-many relation used in this query:
User and Result
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.hotlava.lms.model.User"
table="`User`"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="identity">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<timestamp
name="timestamp"
column="timestamp"
/>
<property
name="username"
type="java.lang.String"
update="true"
insert="true"
column="username"
not-null="true"
unique="true"
/>
<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
not-null="true"
/>
<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="firstName"
/>
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="lastName"
/>
[removed for brevity]
<many-to-one
name="company"
class="com.hotlava.lms.model.Company"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="company"
/>
<set
name="results"
lazy="true"
inverse="true"
cascade="delete"
sort="unsorted"
>
<key
column="`user`"
>
</key>
<one-to-many
class="com.hotlava.lms.model.Result"
/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.hotlava.lms.model.Result"
table="Result"
>
<cache usage="nonstrict-read-write" />
<composite-id
name="id"
class="com.hotlava.lms.model.ResultPK"
>
<key-many-to-one
name="course"
class="com.hotlava.lms.model.Course"
column="course"
/>
<key-many-to-one
name="user"
class="com.hotlava.lms.model.User"
column="`user`"
/>
</composite-id>
<timestamp
name="timestamp"
column="timestamp"
/>
<property
name="lastPage"
type="java.lang.String"
update="true"
insert="true"
column="lastPage"
/>
<property
name="start"
type="java.util.Date"
update="true"
insert="true"
column="start"
/>
<property
name="sessionStart"
type="java.util.Date"
update="true"
insert="true"
column="sessionStart"
/>
<property
name="company"
type="java.lang.Long"
update="true"
insert="true"
column="company"
/>
</class>
</hibernate-mapping>
The generated SQL (show_sql=true):For Criteria query the following SQL is generated:
Code:
select this_.username as y0_, max(re1_.finish) as y1_ from `User` this_
left outer join Result re1_ on this_.id=re1_.`user` group by
this_.username order by y1_ asc