Search in sources :

Example 26 with MetadataImplementor

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

the class SchemaUpdateTask method execute.

/**
	 * Execute the task
	 */
@Override
public void execute() throws BuildException {
    log("Running Hibernate Core SchemaUpdate.");
    log("This is an Ant task supporting only mapping files, if you want to use annotations see http://tools.hibernate.org.");
    try {
        final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder();
        configure(ssrBuilder);
        final StandardServiceRegistry ssr = ssrBuilder.build();
        final MetadataSources metadataSources = new MetadataSources(ssr);
        configure(metadataSources);
        final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
        configure(metadataBuilder, ssr);
        final MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();
        new SchemaUpdate().setOutputFile(outputFile.getPath()).setDelimiter(delimiter).setHaltOnError(haltOnError).execute(TargetTypeHelper.parseLegacyCommandLineOptions(!quiet, !text, outputFile.getPath()), metadata);
    } 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 27 with MetadataImplementor

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

the class SchemaValidator method main.

public static void main(String[] args) {
    try {
        final CommandLineArgs parsedArgs = CommandLineArgs.parseCommandLineArgs(args);
        final StandardServiceRegistry serviceRegistry = buildStandardServiceRegistry(parsedArgs);
        try {
            final MetadataImplementor metadata = buildMetadata(parsedArgs, serviceRegistry);
            new SchemaValidator().validate(metadata, serviceRegistry);
        } finally {
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
    } catch (Exception e) {
        LOG.unableToRunSchemaUpdate(e);
        e.printStackTrace();
    }
}
Also used : MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 28 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 29 with MetadataImplementor

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

the class IndividuallySchemaValidatorImplTest method testMissingEntityContainsQualifiedEntityName.

@Test
public void testMissingEntityContainsQualifiedEntityName() throws Exception {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(MissingEntity.class);
    final MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    try {
        getSchemaValidator(metadata);
        Assert.fail("SchemaManagementException expected");
    } catch (SchemaManagementException e) {
        assertEquals("Schema-validation: missing table [SomeCatalog.SomeSchema.MissingEntity]", e.getMessage());
    }
}
Also used : SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Test(org.junit.Test)

Example 30 with MetadataImplementor

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

the class IndividuallySchemaValidatorImplTest method testMissingColumn.

@Test
public void testMissingColumn() throws Exception {
    MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(NoNameColumn.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    Map<String, Object> settings = new HashMap<>();
    ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
    DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl();
    connectionProvider.configure(properties());
    final GenerationTargetToDatabase schemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, new JdbcConnectionAccessImpl(connectionProvider)));
    try {
        new SchemaCreatorImpl(ssr).doCreation(metadata, serviceRegistry, settings, true, schemaGenerator);
        metadataSources = new MetadataSources(ssr);
        metadataSources.addAnnotatedClass(NameColumn.class);
        metadata = (MetadataImplementor) metadataSources.buildMetadata();
        metadata.validate();
        try {
            getSchemaValidator(metadata);
            Assert.fail("SchemaManagementException expected");
        } catch (SchemaManagementException e) {
            assertEquals("Schema-validation: missing column [name] in table [SomeSchema.ColumnEntity]", e.getMessage());
        }
    } finally {
        new SchemaDropperImpl(serviceRegistry).doDrop(metadata, false, schemaGenerator);
        serviceRegistry.destroy();
        connectionProvider.stop();
    }
}
Also used : JdbcConnectionAccessImpl(org.hibernate.testing.boot.JdbcConnectionAccessImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) Test(org.junit.Test)

Aggregations

MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)68 MetadataSources (org.hibernate.boot.MetadataSources)58 Test (org.junit.Test)53 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)34 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)33 TestForIssue (org.hibernate.testing.TestForIssue)25 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)19 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)17 PersistentClass (org.hibernate.mapping.PersistentClass)15 File (java.io.File)14 MetadataBuilder (org.hibernate.boot.MetadataBuilder)7 Property (org.hibernate.mapping.Property)7 SimpleValue (org.hibernate.mapping.SimpleValue)7 Type (org.hibernate.type.Type)7 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)6 BasicType (org.hibernate.type.BasicType)6 Metadata (org.hibernate.boot.Metadata)4 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)4 Namespace (org.hibernate.boot.model.relational.Namespace)4 Sequence (org.hibernate.boot.model.relational.Sequence)4