javapostgresqlspring-boothibernatehibernate-types

How to use a 'Hibernate Types' library type in a single native query that selects Postgres arrays?


I have a query that returns a Postgres array of UUIDs:

SELECT e.id, e.date,
ARRAY
(
 SELECT cs.api_id FROM event_condition_set ecs
 JOIN condition_set cs on cs.id = ecs.condition_set_id
 WHERE ecs.event_id = e.id
) AS condition_set_ids,
...

And then create and run this query as a native query: Query query = entityManager.createNativeQuery(queryString);

Since Hibernate can normally not deal with these Postgres arrays, I use Vlad's Hibernate Types library.

However, currently I need to register this UUIDArrayType globally in my application:

public class PostgreSQL95CustomDialect extends PostgreSQL95Dialect {

    public PostgreSQL95CustomDialect() {
        super();
        this.registerHibernateType(Types.ARRAY, UUIDArrayType.class.getName());
    }

}

Aside from the fact this is a bit ugly, it also leaves no room for other types of arrays.

(Note I also tried registering a generic ListArrayType but this throws a NullPointerException during execution of the query.)

I have also tried registering it as a scalar type:

query.unwrap(org.hibernate.query.NativeQuery.class)
            .addScalar("condition_set_ids", UUIDArrayType.INSTANCE);

But this makes the entire query only return a single UUID, which is very strange and seems bugged.

Is there a way to ONLY use this UUIDArrayType specifically in this query?

(Please don't suggest using array_agg because the performance is terrible for this case)


Solution

  • Answering my own question here. After waiting for a while and updating to the most recent library version (I'm on 2.19.2 right now) I don't have any issues anymore with the scalar types registration as I mentioned in my question, i.e.:

    query.unwrap(org.hibernate.query.NativeQuery.class)
                .addScalar("condition_set_ids", UUIDArrayType.INSTANCE);
    

    So it appears to just have been a bug and I can now avoid the global registration in favor of using scalars.

    Additional information: after migrating to Spring Boot 3 with Hibernate 6, we don't need this library at all anymore.