Reputation: 1370
I have own table containing car data:
What I do want is to get newest car of every customer. Using a common query, I would order the data by date DESC and then group by customer.
Using criteria, I tried this:
session.createCriteria(Car.class)
.addOrder(Order.desc("CarAge"))
.setProjection( Projections.groupProperty("CustomerID"));
Strangely, this does not return Car objects but it returns a list of customer IDs.
Upvotes: 1
Views: 1372
Reputation: 4030
You cannot expect Car objects.
select * from Car group by customerId order by carAge desc;
The above sqlquery does not work in database as well. Below is the query that is executed by Hibernate.
select customerId from Car group by customerId order by carAge desc;
If you need other columns as well, add it as group property and you will get it, else get all the ids and query the database again to get rest of the values else write HQL to have subquery to return specific customer ids
Upvotes: 2