|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.schemaupdate; |
| 8 | + |
| 9 | +import java.math.BigInteger; |
| 10 | +import java.util.EnumSet; |
| 11 | + |
| 12 | +import org.hibernate.Session; |
| 13 | +import org.hibernate.SessionFactory; |
| 14 | +import org.hibernate.annotations.Array; |
| 15 | +import org.hibernate.boot.Metadata; |
| 16 | +import org.hibernate.boot.MetadataSources; |
| 17 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 18 | +import org.hibernate.cfg.Environment; |
| 19 | +import org.hibernate.tool.hbm2ddl.SchemaExport; |
| 20 | +import org.hibernate.tool.hbm2ddl.SchemaUpdate; |
| 21 | +import org.hibernate.tool.schema.TargetType; |
| 22 | + |
| 23 | +import org.hibernate.testing.orm.junit.Jira; |
| 24 | +import org.hibernate.testing.util.ServiceRegistryUtil; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import jakarta.persistence.Entity; |
| 28 | +import jakarta.persistence.GeneratedValue; |
| 29 | +import jakarta.persistence.Id; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Marco Belladelli |
| 35 | + */ |
| 36 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18406" ) |
| 37 | +public class SchemaUpdateArrayPropertiesTest { |
| 38 | + @Test |
| 39 | + public void testUpdateExisting() { |
| 40 | + final StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder() |
| 41 | + .applySetting( Environment.HBM2DDL_AUTO, "none" ) |
| 42 | + .build(); |
| 43 | + final Metadata metadata = new MetadataSources( ssr ).addAnnotatedClass( TestEntity.class ).buildMetadata(); |
| 44 | + // First create the schema |
| 45 | + new SchemaExport().createOnly( EnumSet.of( TargetType.DATABASE ), metadata ); |
| 46 | + // Then update the existing table |
| 47 | + new SchemaUpdate().execute( EnumSet.of( TargetType.DATABASE ), metadata ); |
| 48 | + // Verify a query works as expected |
| 49 | + try (final SessionFactory sf = metadata.getSessionFactoryBuilder().build()) { |
| 50 | + try (Session session = sf.openSession()) { |
| 51 | + assertThat( session.find( TestEntity.class, 1 ) ).isNull(); |
| 52 | + } |
| 53 | + sf.getSchemaManager().dropMappedObjects( false ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testUpdateNew() { |
| 59 | + final StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder() |
| 60 | + .applySetting( Environment.HBM2DDL_AUTO, "none" ) |
| 61 | + .build(); |
| 62 | + final Metadata metadata = new MetadataSources( ssr ).addAnnotatedClass( TestEntity.class ).buildMetadata(); |
| 63 | + // Update should create the schema and all necessary types |
| 64 | + new SchemaUpdate().execute( EnumSet.of( TargetType.DATABASE ), metadata ); |
| 65 | + // Verify a query works as expected |
| 66 | + try (final SessionFactory sf = metadata.getSessionFactoryBuilder().build()) { |
| 67 | + try (Session session = sf.openSession()) { |
| 68 | + assertThat( session.find( TestEntity.class, 1 ) ).isNull(); |
| 69 | + } |
| 70 | + sf.getSchemaManager().dropMappedObjects( false ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Entity( name = "TestEntity" ) |
| 75 | + static class TestEntity { |
| 76 | + @Id |
| 77 | + @GeneratedValue |
| 78 | + private Integer id; |
| 79 | + |
| 80 | + private Integer[] integerArray; |
| 81 | + |
| 82 | + @Array( length = 3 ) |
| 83 | + private String[] stringArrayAnnotated; |
| 84 | + |
| 85 | + @Array( length = 5 ) |
| 86 | + private BigInteger[] bigIntegerArrayAnnotated; |
| 87 | + } |
| 88 | +} |
0 commit comments