Getting aggregate function column does not exist error in hibernate

Viewed 24

I have one class named MstarHoldings and that is an object referring to the table MstarHoldings in the database which has three primary keys investmentId, start_date, and holder_id. I want to use an aggregate function sum and execute a query. I am trying the following code. I did that and I am trying to fetch single query.

Long sum = (Long) rdsSession.createNativeQuery(DBQueries.GET_FLOAT_SHARES)
                                .setParameter("investmentId", investmentId, StringType.INSTANCE)
                                .setParameter("startDate", filteredStartDate.get(i), LocalDateType.INSTANCE)
                                .setParameter("endDate", filteredEndDate.get(i), LocalDateType.INSTANCE).addEntity(MstarstarHoldings.class)
        
                                .getSingleResult();

The query is as follows.

select sum(holdings) holdings "
        + "where morningstar_investment_id = :investmentId and start_date between :startDate and :endDate

I cannot include holder_id which is the primary key because I want only one result from this query and if I include holder_id, it gives me two records. Now I am getting the following error.

Caused by: org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:103) Caused by: org.postgresql.util.PSQLException: ERROR: column "holdings" does not exist Position: 12

Is there any particular way and get one value without getting this error

0 Answers
Related