Search in sources :

Example 81 with MetadataSources

use of org.hibernate.boot.MetadataSources in project cas by apereo.

the class GenerateDdlCommand method generate.

/**
 * Generate.
 *
 * @param file         the file
 * @param dialect      the dialect
 * @param delimiter    the delimiter
 * @param pretty       the pretty
 * @param dropSchema   the drop schema
 * @param createSchema the create schema
 * @param haltOnError  the halt on error
 */
@CliCommand(value = "generate-ddl", help = "Generate database DDL scripts")
public void generate(@CliOption(key = { "file" }, help = "DDL file to contain to generated script", specifiedDefaultValue = "/etc/cas/config/cas-db-schema.sql", unspecifiedDefaultValue = "/etc/cas/config/cas-db-schema.sql", optionContext = "DDL file to contain to generated script") final String file, @CliOption(key = { "dialect" }, help = "Database dialect class", specifiedDefaultValue = "HSQL", unspecifiedDefaultValue = "HSQL", optionContext = "Database dialect class") final String dialect, @CliOption(key = { "delimiter" }, help = "Delimiter to use for separation of statements when generating SQL", specifiedDefaultValue = ";", unspecifiedDefaultValue = ";", optionContext = "Delimiter to use for separation of statements when generating SQL") final String delimiter, @CliOption(key = { "pretty" }, help = "Format DDL scripts and pretty-print the output", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Format DDL scripts and pretty-print the output") final boolean pretty, @CliOption(key = { "dropSchema" }, help = "Generate DROP SQL statements in the DDL", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Generate DROP statements in the DDL") final boolean dropSchema, @CliOption(key = { "createSchema" }, help = "Generate DROP SQL statements in the DDL", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Generate CREATE SQL statements in the DDL") final boolean createSchema, @CliOption(key = { "haltOnError" }, help = "Halt if an error occurs during the generation process", specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", optionContext = "Halt if an error occurs during the generation process") final boolean haltOnError) {
    final String dialectName = DIALECTS_MAP.getOrDefault(dialect.trim().toUpperCase(), dialect);
    LOGGER.info("Using database dialect class [{}]", dialectName);
    if (!dialectName.contains(".")) {
        LOGGER.warn("Dialect name must be a fully qualified class name. Supported dialects by default are [{}] " + "or you may specify the dialect class directly", DIALECTS_MAP.keySet());
        return;
    }
    final StandardServiceRegistryBuilder svcRegistry = new StandardServiceRegistryBuilder();
    if (StringUtils.isNotBlank(dialectName)) {
        svcRegistry.applySetting(AvailableSettings.DIALECT, dialect);
    }
    final MetadataSources metadata = new MetadataSources(svcRegistry.build());
    REFLECTIONS.getTypesAnnotatedWith(MappedSuperclass.class).forEach(metadata::addAnnotatedClass);
    REFLECTIONS.getTypesAnnotatedWith(Entity.class).forEach(metadata::addAnnotatedClass);
    final SchemaExport export = new SchemaExport();
    export.setDelimiter(delimiter);
    export.setOutputFile(file);
    export.setFormat(pretty);
    export.setHaltOnError(haltOnError);
    export.setManageNamespaces(true);
    final SchemaExport.Action action;
    if (createSchema && dropSchema) {
        action = SchemaExport.Action.BOTH;
    } else if (createSchema) {
        action = SchemaExport.Action.CREATE;
    } else if (dropSchema) {
        action = SchemaExport.Action.DROP;
    } else {
        action = SchemaExport.Action.NONE;
    }
    LOGGER.info("Exporting Database DDL to [{}] using dialect [{}] with export type set to [{}]", file, dialect, action);
    export.execute(EnumSet.of(TargetType.SCRIPT, TargetType.STDOUT), SchemaExport.Action.BOTH, metadata.buildMetadata());
    LOGGER.info("Database DDL is exported to [{}]", file);
}
Also used : Entity(javax.persistence.Entity) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MappedSuperclass(javax.persistence.MappedSuperclass) MetadataSources(org.hibernate.boot.MetadataSources) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 82 with MetadataSources

use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.

the class MetadataHelper method getMetadataSources.

public static MetadataSources getMetadataSources(Configuration configuration) {
    MetadataSources result = null;
    Field metadataSourcesField = getField("metadataSources", configuration);
    if (metadataSourcesField != null) {
        try {
            metadataSourcesField.setAccessible(true);
            result = (MetadataSources) metadataSourcesField.get(configuration);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    if (result == null) {
        result = new MetadataSources();
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) MetadataSources(org.hibernate.boot.MetadataSources)

Example 83 with MetadataSources

use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.

the class ClassMetadataFacadeTest method createSampleEntityPersister.

private TestEntityPersister createSampleEntityPersister() {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    builder.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    ServiceRegistry serviceRegistry = builder.build();
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory();
    RootClass rc = new RootClass(null);
    Table t = new Table("foobar");
    rc.setTable(t);
    Column c = new Column("foo");
    t.addColumn(c);
    ArrayList<Column> keyList = new ArrayList<>();
    keyList.add(c);
    t.createUniqueKey(keyList);
    SimpleValue sv = new SimpleValue(metadata);
    sv.setNullValue("null");
    sv.setTypeName(Integer.class.getName());
    sv.addColumn(c);
    rc.setEntityName("foobar");
    rc.setIdentifier(sv);
    return new TestEntityPersister(rc, sfi, metadata);
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) ArrayList(java.util.ArrayList) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 84 with MetadataSources

use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.

the class TypeFacadeTest method testIsComponentType.

@Test
public void testIsComponentType() {
    IType typeFacade = null;
    ClassType classType = new ClassType();
    typeFacade = FACADE_FACTORY.createType(classType);
    Assert.assertFalse(typeFacade.isComponentType());
    MetadataBuildingOptions mdbo = new MetadataBuilderImpl.MetadataBuildingOptionsImpl(new StandardServiceRegistryBuilder().build());
    MetadataImplementor mdi = (MetadataImplementor) new MetadataBuilderImpl(new MetadataSources()).build();
    ComponentType componentType = new ComponentType(null, new ComponentMetamodel(new Component(mdi, new RootClass(null)), mdbo));
    typeFacade = FACADE_FACTORY.createType(componentType);
    Assert.assertTrue(typeFacade.isComponentType());
}
Also used : RootClass(org.hibernate.mapping.RootClass) ComponentType(org.hibernate.type.ComponentType) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ComponentMetamodel(org.hibernate.tuple.component.ComponentMetamodel) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ClassType(org.hibernate.type.ClassType) Component(org.hibernate.mapping.Component) MetadataBuilderImpl(org.hibernate.boot.internal.MetadataBuilderImpl) IType(org.jboss.tools.hibernate.runtime.spi.IType) MetadataBuildingOptions(org.hibernate.boot.spi.MetadataBuildingOptions) Test(org.junit.Test)

Example 85 with MetadataSources

use of org.hibernate.boot.MetadataSources in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testAddFile.

@Test
public void testAddFile() throws Exception {
    File testFile = File.createTempFile("test", "hbm.xml");
    PrintWriter printWriter = new PrintWriter(testFile);
    printWriter.write(TEST_HBM_XML_STRING);
    printWriter.close();
    MetadataSources metadataSources = MetadataHelper.getMetadataSources(configuration);
    Assert.assertTrue(metadataSources.getXmlBindings().isEmpty());
    Assert.assertSame(configurationFacade, configurationFacade.addFile(testFile));
    Assert.assertFalse(metadataSources.getXmlBindings().isEmpty());
    Binding<?> binding = metadataSources.getXmlBindings().iterator().next();
    Assert.assertEquals(testFile.getAbsolutePath(), binding.getOrigin().getName());
    Assert.assertTrue(testFile.delete());
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

MetadataSources (org.hibernate.boot.MetadataSources)293 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)193 Test (org.junit.Test)192 Metadata (org.hibernate.boot.Metadata)123 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)120 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)76 TestForIssue (org.hibernate.testing.TestForIssue)57 PersistentClass (org.hibernate.mapping.PersistentClass)53 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)41 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)37 Before (org.junit.Before)33 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)30 ServiceRegistry (org.hibernate.service.ServiceRegistry)26 RootClass (org.hibernate.mapping.RootClass)25 HashMap (java.util.HashMap)20 SimpleValue (org.hibernate.mapping.SimpleValue)20 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)20 File (java.io.File)19 Property (org.hibernate.mapping.Property)18 Map (java.util.Map)17