Reputation: 703
What is the best way to add some sql to the select part of a Criteria object?
(I want to add SELECT myFunction AS distance so that I can later order by distance)
Cheers, Rob
Upvotes: 1
Views: 129
Reputation: 5555
List cats = session.createCriteria(Cat.class)
.createAlias("kittens", "kit")
.add( Restrictions.like("kit.name", "Iz%") )
.list();
However I would advise you not to mix Hibernate criteria API with SQL code: choose to use either SQL (or better HQL) code or the criteria API.
Upvotes: 1
Reputation: 357
From Hibernate documentation:
List results = session.createCriteria(Cat.class)
.setProjection( Projections.projectionList()
.add( Projections.rowCount(), "catCountByColor" )
.add( Projections.avg("weight"), "avgWeight" )
.add( Projections.max("weight"), "maxWeight" )
.add( Projections.groupProperty("color"), "color" )
)
.addOrder( Order.desc("catCountByColor") )
.addOrder( Order.desc("avgWeight") )
.list();
Upvotes: 1