Vyacheslav
Vyacheslav

Reputation: 3244

how to create hibernate criteria with group by clause?

I need to create criteria analogous to

select obj, obj.prop.id from Object1 obj group by obj.prop.id

How to do that?

Upvotes: 0

Views: 4698

Answers (1)

gkamal
gkamal

Reputation: 21000

Example from the reference http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html#querycriteria-projection

List results = session.createCriteria(Cat.class)
    .setProjection( Projections.projectionList()
        .add( Projections.rowCount() )
        .add( Projections.avg("weight") )
        .add( Projections.max("weight") )
        .add( Projections.groupProperty("color") )
    )
    .list();

Upvotes: 3

Related Questions