|
| 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.test.schemaupdate; |
| 8 | + |
| 9 | +import javax.persistence.Entity; |
| 10 | +import javax.persistence.Id; |
| 11 | +import javax.persistence.Index; |
| 12 | +import javax.persistence.Table; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import org.hibernate.boot.MetadataSources; |
| 17 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 18 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 19 | +import org.hibernate.cfg.Environment; |
| 20 | +import org.hibernate.dialect.PostgreSQL81Dialect; |
| 21 | +import org.hibernate.service.ServiceRegistry; |
| 22 | +import org.hibernate.tool.hbm2ddl.SchemaExport; |
| 23 | + |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | + |
| 29 | +import org.hibernate.testing.RequiresDialect; |
| 30 | +import org.hibernate.testing.TestForIssue; |
| 31 | +import org.hibernate.testing.junit4.CustomRunner; |
| 32 | + |
| 33 | +import static org.hamcrest.core.Is.is; |
| 34 | +import static org.junit.Assert.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Andrea Boriero |
| 38 | + */ |
| 39 | +@TestForIssue(jiraKey = "HHH-9866") |
| 40 | +@RunWith(CustomRunner.class) |
| 41 | +@RequiresDialect(PostgreSQL81Dialect.class) |
| 42 | +public class SchemaExportWithIndexAndDefaultSchema { |
| 43 | + protected ServiceRegistry serviceRegistry; |
| 44 | + protected MetadataImplementor metadata; |
| 45 | + |
| 46 | + @Test |
| 47 | + public void shouldCreateIndex() { |
| 48 | + SchemaExport schemaExport = new SchemaExport( serviceRegistry, metadata ); |
| 49 | + schemaExport.create( true, true ); |
| 50 | + |
| 51 | + List<SQLException> exceptions = schemaExport.getExceptions(); |
| 52 | + assertThat( exceptions.size(), is( 0 ) ); |
| 53 | + } |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setUp() { |
| 57 | + serviceRegistry = new StandardServiceRegistryBuilder() |
| 58 | + .applySetting( Environment.GLOBALLY_QUOTED_IDENTIFIERS, "true" ) |
| 59 | + .applySetting( Environment.DEFAULT_SCHEMA, "public" ) |
| 60 | + .build(); |
| 61 | + metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) |
| 62 | + .addAnnotatedClass( MyEntity.class ) |
| 63 | + .buildMetadata(); |
| 64 | + |
| 65 | + System.out.println( "********* Starting SchemaExport for START-UP *************************" ); |
| 66 | + SchemaExport schemaExport = new SchemaExport( serviceRegistry, metadata ); |
| 67 | + schemaExport.create( true, true ); |
| 68 | + System.out.println( "********* Completed SchemaExport for START-UP *************************" ); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + @After |
| 73 | + public void tearDown() { |
| 74 | + System.out.println( "********* Starting SchemaExport (drop) for TEAR-DOWN *************************" ); |
| 75 | + SchemaExport schemaExport = new SchemaExport( serviceRegistry, metadata ); |
| 76 | + schemaExport.drop( true, true ); |
| 77 | + System.out.println( "********* Completed SchemaExport (drop) for TEAR-DOWN *************************" ); |
| 78 | + |
| 79 | + StandardServiceRegistryBuilder.destroy( serviceRegistry ); |
| 80 | + serviceRegistry = null; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + @Entity |
| 85 | + @Table(name = "MyEntity", indexes = {@Index(columnList = "id", name = "user_id_hidx")}) |
| 86 | + public static class MyEntity { |
| 87 | + private int id; |
| 88 | + |
| 89 | + @Id |
| 90 | + public int getId() { |
| 91 | + return this.id; |
| 92 | + } |
| 93 | + |
| 94 | + public void setId(final int id) { |
| 95 | + this.id = id; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments