mostar
mostar

Reputation: 4831

Arithmetic Operation with JPA on a DB Field of Type String

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");

Upvotes: 0

Views: 351

Answers (1)

NPKR
NPKR

Reputation: 5506

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.

Upvotes: 1

Related Questions