|
| 1 | +package org.hibernate.orm.test.schemaupdate; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.util.EnumSet; |
| 6 | + |
| 7 | +import org.hibernate.boot.MetadataSources; |
| 8 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 9 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 10 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 11 | +import org.hibernate.dialect.MySQLDialect; |
| 12 | +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; |
| 13 | +import org.hibernate.tool.hbm2ddl.SchemaExport; |
| 14 | +import org.hibernate.tool.hbm2ddl.SchemaValidator; |
| 15 | +import org.hibernate.tool.schema.TargetType; |
| 16 | + |
| 17 | +import org.hibernate.testing.orm.junit.BaseUnitTest; |
| 18 | +import org.hibernate.testing.orm.junit.Jira; |
| 19 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 20 | +import org.hibernate.testing.util.ServiceRegistryUtil; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import jakarta.persistence.Entity; |
| 26 | +import jakarta.persistence.Id; |
| 27 | +import jakarta.persistence.Lob; |
| 28 | +import jakarta.persistence.Table; |
| 29 | + |
| 30 | +@Jira("HHH-16094") |
| 31 | +@BaseUnitTest |
| 32 | +@RequiresDialect(MySQLDialect.class) |
| 33 | +public class MySQLLobSchemaValidationTest { |
| 34 | + private StandardServiceRegistry ssr; |
| 35 | + private MetadataImplementor metadata; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setUp() throws Exception { |
| 39 | + ssr = ServiceRegistryUtil.serviceRegistry(); |
| 40 | + ConnectionProvider connectionProvider = ssr.getService( ConnectionProvider.class ); |
| 41 | + try (Connection connection = connectionProvider.getConnection();) { |
| 42 | + PreparedStatement statement = connection.prepareStatement( |
| 43 | + "create table TestEntity (id integer not null, lobField longtext, primary key (id))" ); |
| 44 | + try { |
| 45 | + statement.executeUpdate(); |
| 46 | + } |
| 47 | + finally { |
| 48 | + statement.close(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @AfterEach |
| 55 | + public void tearsDown() { |
| 56 | + dropSchema( TestEntity.class ); |
| 57 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testSchemaValidation() { |
| 62 | + final MetadataSources metadataSources = new MetadataSources( ssr ); |
| 63 | + |
| 64 | + metadataSources.addAnnotatedClass( TestEntity.class ); |
| 65 | + metadata = (MetadataImplementor) metadataSources.buildMetadata(); |
| 66 | + metadata.orderColumns( false ); |
| 67 | + metadata.validate(); |
| 68 | + new SchemaValidator().validate( metadata ); |
| 69 | + } |
| 70 | + |
| 71 | + private void dropSchema(Class... annotatedClasses) { |
| 72 | + final MetadataSources metadataSources = new MetadataSources( ssr ); |
| 73 | + |
| 74 | + for ( Class c : annotatedClasses ) { |
| 75 | + metadataSources.addAnnotatedClass( c ); |
| 76 | + } |
| 77 | + metadata = (MetadataImplementor) metadataSources.buildMetadata(); |
| 78 | + metadata.orderColumns( false ); |
| 79 | + metadata.validate(); |
| 80 | + new SchemaExport() |
| 81 | + .setHaltOnError( false ) |
| 82 | + .setFormat( false ) |
| 83 | + .drop( EnumSet.of( TargetType.DATABASE ), metadata ); |
| 84 | + } |
| 85 | + |
| 86 | + @Entity |
| 87 | + @Table(name = "TestEntity") |
| 88 | + public static class TestEntity { |
| 89 | + |
| 90 | + @Id |
| 91 | + private int id; |
| 92 | + |
| 93 | + @Lob |
| 94 | + private String lobField; |
| 95 | + |
| 96 | + } |
| 97 | +} |
0 commit comments