0

I have a table like below:

       user_id   number
        1         10
        2         17
        1         12
        2         18

I mean one user can have more than one row. I have to reach following result:

       user_id   number
        1         12
        2         18

How can I achive this with hibernate criteria?

Gary Leather
  • 281
  • 2
  • 6
  • 18

2 Answers2

4

You can try this.

Criteria cr = session.createCriteria(User.class);
cr.setProjection(
         Projections.projectionList()
            .add(Projections.groupProperty("userId"))
            .add(Projections.max("number"))
);

This is an untested code.

NewUser
  • 3,729
  • 10
  • 57
  • 79
2
select u.id, max(u.number) from User u group by u.id

This is the HQL query you need. I don't see any point of translating this to criteria, but if you really want to do it, it shouldn't be hard. You'll need to use projections from the Projections class.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255