Search in sources :

Example 81 with MetadataImplementor

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

the class SchemaExportTask method doExecution.

private void doExecution() throws Exception {
    final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder(bsr);
    final MetadataSources metadataSources = new MetadataSources(bsr);
    if (configurationFile != null) {
        ssrBuilder.configure(configurationFile);
    }
    if (propertiesFile != null) {
        ssrBuilder.loadProperties(propertiesFile);
    }
    ssrBuilder.applySettings(getProject().getProperties());
    for (String fileName : getFiles()) {
        if (fileName.endsWith(".jar")) {
            metadataSources.addJar(new File(fileName));
        } else {
            metadataSources.addFile(fileName);
        }
    }
    ssrBuilder.applySetting(AvailableSettings.HBM2DDL_DELIMITER, delimiter);
    ExportType exportType = ExportType.interpret(drop, create);
    Target output = Target.interpret(!quiet, !text);
    if (output.doScript()) {
        ssrBuilder.applySetting(AvailableSettings.HBM2DDL_SCRIPTS_ACTION, exportType.getAction());
        final Object scriptTarget;
        if (outputFile == null) {
            scriptTarget = new OutputStreamWriter(System.out);
        } else {
            scriptTarget = outputFile;
        }
        if (exportType.doCreate()) {
            ssrBuilder.applySetting(AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET, scriptTarget);
        }
        if (exportType.doDrop()) {
            ssrBuilder.applySetting(AvailableSettings.HBM2DDL_SCRIPTS_DROP_TARGET, scriptTarget);
        }
    }
    if (output.doExport()) {
        ssrBuilder.applySetting(AvailableSettings.HBM2DDL_DATABASE_ACTION, exportType.getAction());
    }
    final StandardServiceRegistryImpl ssr = (StandardServiceRegistryImpl) ssrBuilder.build();
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(ssr);
    ClassLoaderService classLoaderService = bsr.getService(ClassLoaderService.class);
    if (implicitNamingStrategy != null) {
        metadataBuilder.applyImplicitNamingStrategy((ImplicitNamingStrategy) classLoaderService.classForName(implicitNamingStrategy).newInstance());
    }
    if (physicalNamingStrategy != null) {
        metadataBuilder.applyPhysicalNamingStrategy((PhysicalNamingStrategy) classLoaderService.classForName(physicalNamingStrategy).newInstance());
    }
    final MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();
    metadata.validate();
    SchemaManagementToolCoordinator.process(metadata, ssr, ssr.getService(ConfigurationService.class).getSettings(), DelayedDropRegistryNotAvailableImpl.INSTANCE);
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 82 with MetadataImplementor

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

the class SchemaValidator method buildMetadata.

private static MetadataImplementor buildMetadata(CommandLineArgs parsedArgs, StandardServiceRegistry serviceRegistry) throws Exception {
    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    for (String filename : parsedArgs.hbmXmlFiles) {
        metadataSources.addFile(filename);
    }
    for (String filename : parsedArgs.jarFiles) {
        metadataSources.addJar(new File(filename));
    }
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    final StrategySelector strategySelector = serviceRegistry.getService(StrategySelector.class);
    if (parsedArgs.implicitNamingStrategy != null) {
        metadataBuilder.applyImplicitNamingStrategy(strategySelector.resolveStrategy(ImplicitNamingStrategy.class, parsedArgs.implicitNamingStrategy));
    }
    if (parsedArgs.physicalNamingStrategy != null) {
        metadataBuilder.applyPhysicalNamingStrategy(strategySelector.resolveStrategy(PhysicalNamingStrategy.class, parsedArgs.physicalNamingStrategy));
    }
    return (MetadataImplementor) metadataBuilder.build();
}
Also used : ImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy) MetadataBuilder(org.hibernate.boot.MetadataBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) File(java.io.File) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy)

Example 83 with MetadataImplementor

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

the class CompositeNaturalIdMappingTest method test.

@Test
public void test() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        Metadata meta = new MetadataSources(ssr).addAnnotatedClass(PostalCarrier.class).addAnnotatedClass(PostalCode.class).buildMetadata();
        ((MetadataImplementor) meta).validate();
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 84 with MetadataImplementor

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

the class MigrationTest method testSimpleColumnAddition.

@Test
public void testSimpleColumnAddition() {
    String resource1 = "org/hibernate/test/schemaupdate/1_Version.hbm.xml";
    String resource2 = "org/hibernate/test/schemaupdate/2_Version.hbm.xml";
    MetadataImplementor v1metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addResource(resource1).buildMetadata();
    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), v1metadata);
    final SchemaUpdate v1schemaUpdate = new SchemaUpdate();
    v1schemaUpdate.execute(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), v1metadata);
    assertEquals(0, v1schemaUpdate.getExceptions().size());
    MetadataImplementor v2metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addResource(resource2).buildMetadata();
    final SchemaUpdate v2schemaUpdate = new SchemaUpdate();
    v2schemaUpdate.execute(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), v2metadata);
    assertEquals(0, v2schemaUpdate.getExceptions().size());
    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), v2metadata);
}
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)

Example 85 with MetadataImplementor

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

the class MigrationTest method testSameTableNameDifferentExplicitSchemas.

@Test
@TestForIssue(jiraKey = "HHH-9550")
public void testSameTableNameDifferentExplicitSchemas() {
    MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addAnnotatedClass(CustomerInfo.class).addAnnotatedClass(PersonInfo.class).buildMetadata();
    // drop and then create the schema
    new SchemaExport().execute(EnumSet.of(TargetType.DATABASE), SchemaExport.Action.BOTH, metadata);
    try {
        // update the schema
        new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
    } finally {
        // drop the schema
        new SchemaExport().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)

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