|
| 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.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.hibernate.testing.orm.junit.Setting; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.GeneratedValue; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import org.assertj.core.data.Index; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.assertj.core.api.Assertions.fail; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Marco Belladelli |
| 30 | + */ |
| 31 | +public class ArrayOfArraysTest { |
| 32 | + @DomainModel( annotatedClasses = ArrayOfArraysTest.EntityWithDoubleByteArray.class ) |
| 33 | + @SessionFactory |
| 34 | + @ServiceRegistry( settings = @Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "create-drop" ) ) |
| 35 | + @Test |
| 36 | + public void testDoubleByteArrayWorks(SessionFactoryScope scope) { |
| 37 | + final Long id = scope.fromTransaction( session -> { |
| 38 | + final EntityWithDoubleByteArray entity = new EntityWithDoubleByteArray(); |
| 39 | + entity.setByteArray( new byte[][] { new byte[] { 1 } } ); |
| 40 | + session.persist( entity ); |
| 41 | + return entity.getId(); |
| 42 | + } ); |
| 43 | + scope.inSession( session -> { |
| 44 | + final byte[][] byteArray = session.find( EntityWithDoubleByteArray.class, id ).getByteArray(); |
| 45 | + assertThat( byteArray ).hasDimensions( 1, 1 ).contains( new byte[] { 1 }, Index.atIndex( 0 ) ); |
| 46 | + } ); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testDoubleIntegerArrayThrows() { |
| 51 | + final Configuration cfg = new Configuration(); |
| 52 | + cfg.addAnnotatedClass( EntityWithDoubleIntegerArray.class ); |
| 53 | + try (final org.hibernate.SessionFactory sf = cfg.buildSessionFactory()) { |
| 54 | + fail( "Expecting Integer[][] to trigger exception as non-byte multidimensional arrays are not supported" ); |
| 55 | + } |
| 56 | + catch (Exception e) { |
| 57 | + assertThat( e ).isInstanceOf( MappingException.class ).hasMessage( "Nested arrays (with the exception of byte[][]) are not supported" ); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Entity( name = "EntityWithDoubleByteArray" ) |
| 62 | + static class EntityWithDoubleByteArray { |
| 63 | + @Id |
| 64 | + @GeneratedValue |
| 65 | + private Long id; |
| 66 | + |
| 67 | + @JdbcTypeCode( SqlTypes.ARRAY ) |
| 68 | + private byte[][] byteArray; |
| 69 | + |
| 70 | + public Long getId() { |
| 71 | + return id; |
| 72 | + } |
| 73 | + |
| 74 | + public byte[][] getByteArray() { |
| 75 | + return byteArray; |
| 76 | + } |
| 77 | + |
| 78 | + public void setByteArray(byte[][] byteArray) { |
| 79 | + this.byteArray = byteArray; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Entity( name = "EntityWithDoubleIntegerArray" ) |
| 84 | + static class EntityWithDoubleIntegerArray { |
| 85 | + @Id |
| 86 | + @GeneratedValue |
| 87 | + private Long id; |
| 88 | + |
| 89 | + @JdbcTypeCode( SqlTypes.ARRAY ) |
| 90 | + private Integer[][] integers; |
| 91 | + } |
| 92 | +} |
0 commit comments