Search in sources :

Example 51 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class SchemaValidatorTask method execute.

/**
 * Execute the task
 */
@Override
public void execute() throws BuildException {
    try {
        final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder();
        configure(ssrBuilder);
        final StandardServiceRegistry ssr = ssrBuilder.build();
        try {
            final MetadataSources metadataSources = new MetadataSources(ssrBuilder.build());
            configure(metadataSources);
            final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
            configure(metadataBuilder, ssr);
            final MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();
            new SchemaValidator().validate(metadata, ssr);
        } finally {
            StandardServiceRegistryBuilder.destroy(ssr);
        }
    } catch (HibernateException e) {
        throw new BuildException("Schema text failed: " + e.getMessage(), e);
    } catch (FileNotFoundException e) {
        throw new BuildException("File not found: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new BuildException("IOException : " + e.getMessage(), e);
    } catch (BuildException e) {
        throw e;
    } catch (Exception e) {
        throw new BuildException(e);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) HibernateException(org.hibernate.HibernateException) MetadataSources(org.hibernate.boot.MetadataSources) FileNotFoundException(java.io.FileNotFoundException) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) FileNotFoundException(java.io.FileNotFoundException) HibernateException(org.hibernate.HibernateException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 52 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class HANASchemaMigrationTargetScriptCreationTest method tearDown.

@After
public void tearDown() {
    ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(Environment.getProperties());
    try {
        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addAnnotatedClass(TestEntity.class).buildMetadata();
        metadata.validate();
        new SchemaExport().drop(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), metadata);
    } finally {
        ServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ServiceRegistry(org.hibernate.service.ServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) After(org.junit.After)

Example 53 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class SqlServerQuoteSchemaTest method test.

@Test
public void test() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE.toString()).build();
    try {
        output.deleteOnExit();
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(MyEntity.class).buildMetadata();
        metadata.validate();
        new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setDelimiter(";").setFormat(true).execute(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
    try {
        String fileContent = new String(Files.readAllBytes(output.toPath()));
        Pattern fileContentPattern = Pattern.compile("create table \\[my\\-schema\\]\\.\\[my_entity\\]");
        Matcher fileContentMatcher = fileContentPattern.matcher(fileContent.toLowerCase());
        assertThat(fileContentMatcher.find(), is(true));
    } catch (IOException e) {
        fail(e.getMessage());
    }
    ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE.toString()).build();
    try {
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(MyEntityUpdated.class).buildMetadata();
        metadata.validate();
        new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setDelimiter(";").setFormat(true).execute(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
    try {
        String fileContent = new String(Files.readAllBytes(output.toPath()));
        Pattern fileContentPattern = Pattern.compile("alter table .*?\\.\\[my\\-schema\\]\\.\\[my_entity\\]");
        Matcher fileContentMatcher = fileContentPattern.matcher(fileContent.toLowerCase());
        assertThat(fileContentMatcher.find(), is(true));
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : Pattern(java.util.regex.Pattern) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Matcher(java.util.regex.Matcher) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) IOException(java.io.IOException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 54 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class SchemaExportTest method testHibernateMappingSchemaPropertyIsNotIgnored.

@Test
@TestForIssue(jiraKey = "HHH-10678")
@RequiresDialectFeature(value = DialectChecks.SupportSchemaCreation.class)
public void testHibernateMappingSchemaPropertyIsNotIgnored() throws Exception {
    File output = File.createTempFile("update_script", ".sql");
    output.deleteOnExit();
    final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addResource("org/hibernate/test/schemaupdate/mapping2.hbm.xml").buildMetadata();
    metadata.validate();
    final SchemaExport schemaExport = new SchemaExport();
    schemaExport.setOutputFile(output.getAbsolutePath());
    schemaExport.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata);
    String fileContent = new String(Files.readAllBytes(output.toPath()));
    Pattern fileContentPattern = Pattern.compile("create( (column|row))? table schema1.version");
    Matcher fileContentMatcher = fileContentPattern.matcher(fileContent.toLowerCase());
    assertThat(fileContent, fileContentMatcher.find(), is(true));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) File(java.io.File) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) TestForIssue(org.hibernate.testing.TestForIssue)

Example 55 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project hibernate-orm by hibernate.

the class SchemaUpdateFormatterTest method testSetFormat.

@Test
public void testSetFormat() throws Exception {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(Environment.HBM2DDL_AUTO, "none").build();
    try {
        File output = File.createTempFile("update_script", ".sql");
        output.deleteOnExit();
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(TestEntity.class).buildMetadata();
        metadata.validate();
        new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setDelimiter(DELIMITER).setFormat(true).execute(EnumSet.of(TargetType.SCRIPT), metadata);
        String outputContent = new String(Files.readAllBytes(output.toPath()));
        // Old Macs use \r as a new line delimiter
        outputContent = outputContent.replaceAll("\r", "\n");
        // On Windows, \r\n would become \n\n, so we eliminate duplicates
        outputContent = outputContent.replaceAll("\n\n", "\n");
        Assert.assertTrue(Pattern.compile(AFTER_FORMAT).matcher(outputContent).matches());
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) File(java.io.File) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)113 MetadataSources (org.hibernate.boot.MetadataSources)80 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)62 Test (org.junit.Test)56 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)52 SimpleValue (org.hibernate.mapping.SimpleValue)32 RootClass (org.hibernate.mapping.RootClass)31 PersistentClass (org.hibernate.mapping.PersistentClass)27 TestForIssue (org.hibernate.testing.TestForIssue)26 Table (org.hibernate.mapping.Table)23 Test (org.junit.jupiter.api.Test)23 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)22 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)20 Column (org.hibernate.mapping.Column)17 ArrayList (java.util.ArrayList)15 ServiceRegistry (org.hibernate.service.ServiceRegistry)15 File (java.io.File)14 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)14 Configuration (org.hibernate.cfg.Configuration)12 Property (org.hibernate.mapping.Property)11