None
None

Reputation: 9247

How to use string_agg?

I have this method:

   public List<Object[]> allIncomeChannels(final List<String> list) {

        return entityManager.createQuery(
                "select  string_agg(a.logicalUnitIdent,',') idents, a.incomeChannel.code, a.logicalUnitIdent, a.keyword from IncomeChannelMapEntity a GROUP BY a.incomeChannel.code, a.logicalUnitCode,a.keyword",
                Object[].class).getResultList();

    }

But im getting error :

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: idents near line 1, column 44

Any suggestion how can i fix this? Im using postgres

Upvotes: 0

Views: 2583

Answers (2)

Mushif Ali Nawaz
Mushif Ali Nawaz

Reputation: 3866

Try using createNativeQuery() method with a native query. Here is your updated code:

public List<Object[]> allIncomeChannels(final List<String> list) {
    return entityManager.createNativeQuery(
            "select  string_agg(a.logicalUnitIdent,',') idents, a.incomeChannel.code, a.logicalUnitIdent, a.keyword from IncomeChannelMapEntity a GROUP BY a.incomeChannel.code, a.logicalUnitCode,a.keyword")
            .getResultList();
}

Replace the query with a native query here.

Upvotes: 1

Sounak Saha
Sounak Saha

Reputation: 943

In your query 'as' is missing after string_agg(a.logicalUnitIdent,',') function. It would be like this

"select  string_agg(a.logicalUnitIdent,',') as idents, a.incomeChannel.code,......"

Upvotes: 0

Related Questions