Skip to content

Commit a34fb6e

Browse files
committed
HHH-18582 Add test for issue
1 parent e3e7db0 commit a34fb6e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.array;
6+
7+
import org.hibernate.MappingException;
8+
import org.hibernate.annotations.JdbcTypeCode;
9+
import org.hibernate.cfg.AvailableSettings;
10+
import org.hibernate.cfg.Configuration;
11+
import org.hibernate.type.SqlTypes;
12+
13+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
16+
import org.hibernate.testing.orm.junit.ServiceRegistry;
17+
import org.hibernate.testing.orm.junit.SessionFactory;
18+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
19+
import org.hibernate.testing.orm.junit.Setting;
20+
import org.junit.jupiter.api.Test;
21+
22+
import jakarta.persistence.Entity;
23+
import jakarta.persistence.GeneratedValue;
24+
import jakarta.persistence.Id;
25+
import org.assertj.core.data.Index;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.fail;
29+
30+
/**
31+
* @author Marco Belladelli
32+
*/
33+
@RequiresDialectFeature( feature = DialectFeatureChecks.SupportsStandardArrays.class )
34+
public class ArrayOfArraysTest {
35+
@DomainModel( annotatedClasses = ArrayOfArraysTest.EntityWithDoubleByteArray.class )
36+
@SessionFactory
37+
@ServiceRegistry( settings = @Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "create-drop" ) )
38+
@Test
39+
public void testDoubleByteArrayWorks(SessionFactoryScope scope) {
40+
final Long id = scope.fromTransaction( session -> {
41+
final EntityWithDoubleByteArray entity = new EntityWithDoubleByteArray();
42+
entity.setByteArray( new byte[][] { new byte[] { 1 } } );
43+
session.persist( entity );
44+
return entity.getId();
45+
} );
46+
scope.inSession( session -> {
47+
final byte[][] byteArray = session.find( EntityWithDoubleByteArray.class, id ).getByteArray();
48+
assertThat( byteArray ).hasDimensions( 1, 1 ).contains( new byte[] { 1 }, Index.atIndex( 0 ) );
49+
} );
50+
}
51+
52+
@Test
53+
public void testDoubleIntegerArrayThrows() {
54+
final Configuration cfg = new Configuration();
55+
cfg.addAnnotatedClass( EntityWithDoubleIntegerArray.class );
56+
try (final org.hibernate.SessionFactory sf = cfg.buildSessionFactory()) {
57+
fail( "Expecting Integer[][] to trigger exception as non-byte multidimensional arrays are not supported" );
58+
}
59+
catch (Exception e) {
60+
assertThat( e ).isInstanceOf( MappingException.class ).hasMessage( "Nested arrays (with the exception of byte[][]) are not supported" );
61+
}
62+
}
63+
64+
@Entity( name = "EntityWithDoubleByteArray" )
65+
static class EntityWithDoubleByteArray {
66+
@Id
67+
@GeneratedValue
68+
private Long id;
69+
70+
@JdbcTypeCode( SqlTypes.ARRAY )
71+
private byte[][] byteArray;
72+
73+
public Long getId() {
74+
return id;
75+
}
76+
77+
public byte[][] getByteArray() {
78+
return byteArray;
79+
}
80+
81+
public void setByteArray(byte[][] byteArray) {
82+
this.byteArray = byteArray;
83+
}
84+
}
85+
86+
@Entity( name = "EntityWithDoubleIntegerArray" )
87+
static class EntityWithDoubleIntegerArray {
88+
@Id
89+
@GeneratedValue
90+
private Long id;
91+
92+
@JdbcTypeCode( SqlTypes.ARRAY )
93+
private Integer[][] integers;
94+
}
95+
}

0 commit comments

Comments
 (0)