0

Is there any proper way to use arithmetic methods (sum, avg, etc.) of CriteriaBuilder class with a String property (VARCHAR column) of an Entity class?

Here is related field:

@Column(name="GRADE")
private String grade;

And what I want to do is:

Expression ex = criteriaBuilder.avg(root.get("grade");

1 Answer 1

1

It is not possible to use Aggregate Functions on String (VARCHAR) datatype.

In the reffrence of Oracle http://docs.oracle.com/cd/E12839_01/doc.1111/e12048/funcbltag.htm

Normal Usesage of Aggregate Function in Hibernate is by Using hibernate projections

List results = session.createCriteria(SomeClass.class)
    .setProjection( Projections.projectionList()        
        .add( Projections.avg("someCloumn") )       
    )
    .list();

This is also for only for column witch is having datatype as bitint, float and int only.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.