Search in sources :

Example 31 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class SchemaCreationTest method testUniqueConstraintIsCorrectlyGenerated.

@Test
@TestForIssue(jiraKey = "HHH-10553")
public void testUniqueConstraintIsCorrectlyGenerated() throws Exception {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(Element.class);
    metadataSources.addAnnotatedClass(Category.class);
    metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    final SchemaExport schemaExport = new SchemaExport().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setFormat(false);
    schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata);
    final List<String> sqlLines = Files.readAllLines(output.toPath(), Charset.defaultCharset());
    boolean isUniqueConstraintCreated = false;
    for (String statement : sqlLines) {
        assertThat("Should not try to create the unique constraint for the non existing table element", statement.toLowerCase().contains("alter table element"), is(false));
        if (ssr.getService(JdbcEnvironment.class).getDialect() instanceof DB2Dialect) {
            if (statement.toLowerCase().startsWith("create unique index") && statement.toLowerCase().contains("category (code)")) {
                isUniqueConstraintCreated = true;
            }
        } else {
            if (statement.toLowerCase().startsWith("alter table category add constraint") && statement.toLowerCase().contains("unique (code)")) {
                isUniqueConstraintCreated = true;
            }
        }
    }
    assertThat("Unique constraint for table category is not created", isUniqueConstraintCreated, is(true));
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) DB2Dialect(org.hibernate.dialect.DB2Dialect) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 32 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class TestFkUpdating method setUp.

@Before
public void setUp() {
    serviceRegistry = new StandardServiceRegistryBuilder().build();
    metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addAnnotatedClass(User.class).addAnnotatedClass(Role.class).buildMetadata();
    System.out.println("********* Starting SchemaExport for START-UP *************************");
    new SchemaExport().create(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), metadata);
    System.out.println("********* Completed SchemaExport for START-UP *************************");
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Before(org.junit.Before)

Example 33 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class JoinedInheritanceForeignKeyTest method createSchema.

private void createSchema(Class[] annotatedClasses) {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    for (Class c : annotatedClasses) {
        metadataSources.addAnnotatedClass(c);
    }
    metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    new SchemaExport().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setFormat(false).create(EnumSet.of(TargetType.SCRIPT), metadata);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 34 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class CrossSchemaForeignKeyGenerationTest method testSchemaUpdateDoesNotFailResolvingCrossSchemaForeignKey.

@Test
@TestForIssue(jiraKey = "HHH-10802")
public void testSchemaUpdateDoesNotFailResolvingCrossSchemaForeignKey() throws Exception {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(SchemaOneEntity.class);
    metadataSources.addAnnotatedClass(SchemaTwoEntity.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    new SchemaExport().setOutputFile(output.getAbsolutePath()).setFormat(false).create(EnumSet.of(TargetType.DATABASE), metadata);
    new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setFormat(false).execute(EnumSet.of(TargetType.DATABASE), metadata);
    new SchemaExport().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setFormat(false).drop(EnumSet.of(TargetType.DATABASE), metadata);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 35 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class SchemaExportWithGlobalQuotingEnabledTest method tearDown.

@After
public void tearDown() {
    System.out.println("********* Starting SchemaExport (drop) for TEAR-DOWN *************************");
    new SchemaExport().drop(EnumSet.of(TargetType.STDOUT, TargetType.DATABASE), metadata);
    System.out.println("********* Completed SchemaExport (drop) for TEAR-DOWN *************************");
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
    serviceRegistry = null;
}
Also used : SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) After(org.junit.After)

Aggregations

SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)48 MetadataSources (org.hibernate.boot.MetadataSources)31 Test (org.junit.Test)22 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)17 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)17 TestForIssue (org.hibernate.testing.TestForIssue)12 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)9 After (org.junit.After)9 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)8 Before (org.junit.Before)7 File (java.io.File)4 SQLException (java.sql.SQLException)2 Map (java.util.Map)2 Entity (javax.persistence.Entity)2 Table (org.hibernate.mapping.Table)2 ServiceRegistry (org.hibernate.service.ServiceRegistry)2 MidPointNamingStrategy (com.evolveum.midpoint.repo.sql.util.MidPointNamingStrategy)1 Connection (java.sql.Connection)1 EnumSet (java.util.EnumSet)1 MappedSuperclass (javax.persistence.MappedSuperclass)1