diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java index 36be6570faf5..47e97626e141 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java @@ -190,8 +190,11 @@ protected Object[] getArray( } protected X getArray(BasicExtractor extractor, java.sql.Array array, WrapperOptions options) throws SQLException { - if ( array != null && getElementJdbcType() instanceof AggregateJdbcType ) { - final AggregateJdbcType aggregateJdbcType = (AggregateJdbcType) getElementJdbcType(); + final JdbcType jdbcType = getElementJdbcType(); + if (array != null + && jdbcType instanceof AggregateJdbcType + && ((AggregateJdbcType) jdbcType).getEmbeddableMappingType() != null) { + final AggregateJdbcType aggregateJdbcType = (AggregateJdbcType) jdbcType; final EmbeddableMappingType embeddableMappingType = aggregateJdbcType.getEmbeddableMappingType(); final Object rawArray = array.getArray(); final Object[] domainObjects = new Object[Array.getLength( rawArray )]; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayAggregateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayAggregateTest.java index 26b4c00f6bba..7f2d30803873 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayAggregateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayAggregateTest.java @@ -1,11 +1,10 @@ /* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors */ package org.hibernate.orm.test.function.array; +import java.util.Arrays; import java.util.List; import org.hibernate.boot.ResourceStreamLocator; @@ -14,12 +13,14 @@ import org.hibernate.boot.spi.InFlightMetadataCollector; import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.dialect.OracleArrayJdbcType; +import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.dialect.OracleDialect; import org.hibernate.dialect.SpannerDialect; import org.hibernate.engine.jdbc.Size; import org.hibernate.query.criteria.JpaCriteriaQuery; import org.hibernate.query.criteria.JpaRoot; import org.hibernate.query.sqm.NodeBuilder; +import org.hibernate.testing.orm.junit.RequiresDialect; import org.hibernate.type.SqlTypes; import org.hibernate.type.descriptor.java.ArrayJavaType; import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry; @@ -37,6 +38,7 @@ import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SkipForDialect; +import org.hibernate.testing.orm.junit.Jira; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -172,4 +174,21 @@ public void testNonExistingArrayType(SessionFactoryScope scope) { } ); } + @Test + @Jira("https://hibernate.atlassian.net/browse/HHH-19681") + @RequiresDialect(PostgreSQLDialect.class) + public void testJsonBJdbcArray(SessionFactoryScope scope) { + scope.inTransaction( session -> { + String sql = "select groupId, array_agg(json_values) " + + "from (VALUES (1,'[1,2]'::jsonb),(1,'[10,20]'::jsonb)) as row(groupId,json_values) " + + "group by groupId"; + + List result = session.createNativeQuery(sql, Object[].class).getResultList(); + assertEquals(1,result.size()); + assertEquals(2, result.get(0).length); + assertEquals( 1,result.get(0)[0] ); + assertEquals( "[[1, 2], [10, 20]]", Arrays.toString((String[])result.get(0)[1]) ); + } ); + } + }