ANucool Mittal
ANucool Mittal

Reputation: 205

How to use Hibernate SQL projection query?

I have a SQL projection query that returns a list, but I want a single result. Here is my projection query:

Criteria criteria = getCurrentSession().createCriteria(TbLoyaltytrans.class)
  .add(Restrictions.eq("custid", custId))
  .setProjection(Projections.projectionList()
  .add(Projections.sum("pointin").as("pointin"))
  .add(Projections.sum("pointout").as("pointout"))
  .add(Projections.sqlProjection(
        "(pointin - pointout) as points",  
        new String[]{"points"}, 
        new org.hibernate.type.DoubleType[]{
              new org.hibernate.type.DoubleType()
        }),
        "points")
  );

I want a single object, i.e.: the count from the above query.

What I did wrong with the above query?

Upvotes: 1

Views: 3250

Answers (1)

mejoz
mejoz

Reputation: 135

I assume your are using criteria.list() in order to get the list. You can use criteria.uniqueResult() and it will return a single Object or null.

Upvotes: 1

Related Questions