2

We use Postgres jsonb_agg function in criteria query API like this:

cb.function(
                JSONB_AGG_FUNCTION_NAME,
                JsonNode.class,
                someJoin.get(someField)
            )

and map a result into projection with constructor:

Projection (<some args>, com.fasterxml.jackson.databind.JsonNode node)

In Hibernate 5 we registered jsonb_agg function using this approach:

public class ExtendedPgDialect extends PostgreSQLDialect {

public static final String JSONB_AGG_FUNCTION_NAME = "jsonb_agg";

public ExtendedPgDialect() {
    registerFunction(JSONB_AGG_FUNCTION_NAME,
            new StandardSQLFunction(JSONB_AGG_FUNCTION_NAME, JsonNodeBinaryType.INSTANCE));
     }
}

But in Hibernate 6.6.33.Final we can't register that function and can't use it for querying data. Also we used (JsonNodeBinaryType) type from io.hypersistence:hypersistence-utils-hibernate-53 library. How to register jsonb_agg function in Hibernate 6 for mapping to JsonNode java type?

1 Answer 1

0

Found a solution, may be it can be useful for someone.

public class ExtendedPgDialect extends PostgreSQLDialect {

    @Override
    protected void registerColumnTypes(TypeContributions typeContributions,
                                       ServiceRegistry serviceRegistry) {
        super.registerColumnTypes(typeContributions, serviceRegistry);
        typeContributions.contributeType(JsonNodeBinaryType.INSTANCE);
    }
}

So I have to register io.hypersistence.utils.hibernate.type.json.JsonNodeBinaryType for Hibernate without registering any function like that and now Hibernate can map jsonb column to JsonNode java field. My result generated query :

select
        <some columns>,
        jsonb_agg(<some column>)
    from <some table> ...
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.