|
| 1 | +package org.hibernate.orm.test.schemavalidation; |
| 2 | + |
| 3 | +import org.hibernate.boot.MetadataSources; |
| 4 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 5 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 6 | +import org.hibernate.cfg.AvailableSettings; |
| 7 | +import org.hibernate.dialect.MySQLDialect; |
| 8 | +import org.hibernate.tool.hbm2ddl.SchemaValidator; |
| 9 | + |
| 10 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 11 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 12 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 13 | +import org.hibernate.testing.transaction.TransactionUtil; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import jakarta.persistence.Column; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.EnumType; |
| 21 | +import jakarta.persistence.Enumerated; |
| 22 | +import jakarta.persistence.GeneratedValue; |
| 23 | +import jakarta.persistence.Id; |
| 24 | +import jakarta.persistence.Table; |
| 25 | + |
| 26 | +import static jakarta.persistence.GenerationType.IDENTITY; |
| 27 | + |
| 28 | +@JiraKey("HHH-16498") |
| 29 | +@RequiresDialect(MySQLDialect.class) |
| 30 | +public class MySqlExistingEnumColumnValidationTest extends BaseCoreFunctionalTestCase { |
| 31 | + |
| 32 | + private StandardServiceRegistry ssr; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Class<?>[] getAnnotatedClasses() { |
| 36 | + return new Class<?>[] { EntityE.class }; |
| 37 | + } |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() { |
| 41 | + TransactionUtil.doInHibernate( this::sessionFactory, session -> { |
| 42 | + session.createNativeQuery( "DROP TABLE IF EXISTS en CASCADE" ).executeUpdate(); |
| 43 | + session.createNativeQuery( |
| 44 | + "CREATE TABLE en (id INTEGER NOT NULL AUTO_INCREMENT, sign_position enum ('AFTER_NO_SPACE','AFTER_WITH_SPACE','BEFORE_NO_SPACE','BEFORE_WITH_SPACE'), PRIMARY KEY (id))" ) |
| 45 | + .executeUpdate(); |
| 46 | + } ); |
| 47 | + } |
| 48 | + |
| 49 | + @After |
| 50 | + public void tearDown() { |
| 51 | + TransactionUtil.doInHibernate( this::sessionFactory, session -> { |
| 52 | + session.createNativeQuery( "DROP TABLE en CASCADE" ).executeUpdate(); |
| 53 | + } ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testSynonymUsingGroupedSchemaValidator() { |
| 58 | + ssr = new StandardServiceRegistryBuilder() |
| 59 | + .applySetting( AvailableSettings.HBM2DDL_AUTO, "validate" ) |
| 60 | + .build(); |
| 61 | + try { |
| 62 | + final MetadataSources metadataSources = new MetadataSources( ssr ); |
| 63 | + metadataSources.addAnnotatedClass( EntityE.class ); |
| 64 | + |
| 65 | + new SchemaValidator().validate( metadataSources.buildMetadata() ); |
| 66 | + } |
| 67 | + finally { |
| 68 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Entity(name = "en") |
| 74 | + @Table(name = "en") |
| 75 | + public static class EntityE { |
| 76 | + @Id |
| 77 | + @GeneratedValue(strategy = IDENTITY) |
| 78 | + @Column(name = "id", nullable = false, updatable = false) |
| 79 | + private Integer id; |
| 80 | + |
| 81 | + @Enumerated(EnumType.STRING) |
| 82 | + @Column(name = "sign_position") |
| 83 | + private SignPosition signPosition; |
| 84 | + |
| 85 | + public Integer getId() { |
| 86 | + return id; |
| 87 | + } |
| 88 | + |
| 89 | + public void setId(Integer id) { |
| 90 | + this.id = id; |
| 91 | + } |
| 92 | + |
| 93 | + public SignPosition getSignPosition() { |
| 94 | + return signPosition; |
| 95 | + } |
| 96 | + |
| 97 | + public void setSignPosition(SignPosition signPosition) { |
| 98 | + this.signPosition = signPosition; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static enum SignPosition { |
| 103 | + AFTER_NO_SPACE, AFTER_WITH_SPACE, BEFORE_NO_SPACE, BEFORE_WITH_SPACE |
| 104 | + } |
| 105 | +} |
0 commit comments