Hibernation Criteria Request - Class Exception

I use the Hibernate (3.2) criteria to query, and this gives me an exception when converting to a list.

See my code and exception below:

    List<Summary> summaryList;
    Criteria criteria = session.createCriteria(Summary.class);
    session.beginTransaction();
    summaryList = Criteria.setProjection(
      Projections.projectionList().add(Projections.sum("contractDollar"))
      .add(Projections.groupProperty("department"))).list() ;

exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be added to com.abc.model.Summary

I am not sure why the result is returned as an “Object”, although I indicated it as my pojo (Summary)

Could you help me with this. I'm new to hibernation.

Thanks, Raj.

+3
source share
2 answers

Summary bean, ? , . session.createCriteria().

, :

  • ( bean, ).
  • .
  • 'aliasToBean'.

:

summaryList = Criteria.setProjection(
 Projections.projectionList()
  .add(Projections.sum("contractDollar"), "contractSum")
  .add(Projections.groupProperty("department"))
 )
.setResultTransformer(Transformers.aliasToBean(Summary.class))
.list() ;

Summary bean setContractSum().

+3

, Summary , :

Criteria criteria = session.createCriteria(Summary.class);
Transaction tx = session.beginTransaction();
summaryList = criteria.setProjection(
    Projections.projectionList().add(Projections.sum("contractDollar"))
      .add(Projections.groupProperty("department"))).list() ;

Object[] result = criteria.list().get(0);
// result[0] holds the sum of contractDollar

tx.commit();
+2

Source: https://habr.com/ru/post/1719398/


All Articles