Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.hibernate.HibernateException;
import org.hibernate.Internal;
import org.hibernate.MappingException;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
Expand Down Expand Up @@ -156,16 +157,24 @@ private <J> BasicType<J> resolvedType(JavaType<J> javaType, JdbcType jdbcType) {
}

private <E> BasicType<?> resolvedType(ArrayJdbcType arrayType, BasicPluralJavaType<E> castPluralJavaType) {
final BasicType<E> elementType = resolve( castPluralJavaType.getElementJavaType(), arrayType.getElementJdbcType() );
final BasicType<?> resolvedType = castPluralJavaType.resolveType(
typeConfiguration,
typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(),
resolve( castPluralJavaType.getElementJavaType(), arrayType.getElementJdbcType() ),
elementType,
null,
typeConfiguration.getCurrentBaseSqlTypeIndicators()
);
if ( resolvedType instanceof BasicPluralType<?,?> ) {
register( resolvedType );
}
else if ( resolvedType == null ) {
final Class<?> elementJavaTypeClass = elementType.getJavaTypeDescriptor().getJavaTypeClass();
if ( elementJavaTypeClass != null && elementJavaTypeClass.isArray() && elementJavaTypeClass != byte[].class ) {
// No support for nested arrays, except for byte[][]
throw new MappingException( "Nested arrays (with the exception of byte[][]) are not supported" );
}
}
return resolvedType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.mapping.array;

import org.hibernate.MappingException;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.type.SqlTypes;

import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.assertj.core.data.Index;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

/**
* @author Marco Belladelli
*/
@RequiresDialectFeature( feature = DialectFeatureChecks.SupportsStandardArrays.class )
public class ArrayOfArraysTest {
@DomainModel( annotatedClasses = ArrayOfArraysTest.EntityWithDoubleByteArray.class )
@SessionFactory
@ServiceRegistry( settings = @Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "create-drop" ) )
@Test
public void testDoubleByteArrayWorks(SessionFactoryScope scope) {
final Long id = scope.fromTransaction( session -> {
final EntityWithDoubleByteArray entity = new EntityWithDoubleByteArray();
entity.setByteArray( new byte[][] { new byte[] { 1 } } );
session.persist( entity );
return entity.getId();
} );
scope.inSession( session -> {
final byte[][] byteArray = session.find( EntityWithDoubleByteArray.class, id ).getByteArray();
assertThat( byteArray ).hasDimensions( 1, 1 ).contains( new byte[] { 1 }, Index.atIndex( 0 ) );
} );
}

@Test
public void testDoubleIntegerArrayThrows() {
final Configuration cfg = new Configuration();
cfg.addAnnotatedClass( EntityWithDoubleIntegerArray.class );
try (final org.hibernate.SessionFactory sf = cfg.buildSessionFactory()) {
fail( "Expecting Integer[][] to trigger exception as non-byte multidimensional arrays are not supported" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( MappingException.class ).hasMessage( "Nested arrays (with the exception of byte[][]) are not supported" );
}
}

@Entity( name = "EntityWithDoubleByteArray" )
static class EntityWithDoubleByteArray {
@Id
@GeneratedValue
private Long id;

@JdbcTypeCode( SqlTypes.ARRAY )
private byte[][] byteArray;

public Long getId() {
return id;
}

public byte[][] getByteArray() {
return byteArray;
}

public void setByteArray(byte[][] byteArray) {
this.byteArray = byteArray;
}
}

@Entity( name = "EntityWithDoubleIntegerArray" )
static class EntityWithDoubleIntegerArray {
@Id
@GeneratedValue
private Long id;

@JdbcTypeCode( SqlTypes.ARRAY )
private Integer[][] integers;
}
}
Loading