Search in sources :

Example 91 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder 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 92 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project ignite by apache.

the class HibernateL2CacheSelfTest method startHibernate.

/**
 * Starts Hibernate.
 *
 * @param accessType Cache access type.
 * @param igniteInstanceName Ignite instance name.
 * @return Session factory.
 */
private SessionFactory startHibernate(org.hibernate.cache.spi.access.AccessType accessType, String igniteInstanceName) {
    StandardServiceRegistryBuilder builder = registryBuilder();
    for (Map.Entry<String, String> e : hibernateProperties(igniteInstanceName, accessType.name()).entrySet()) builder.applySetting(e.getKey(), e.getValue());
    // Use the same cache for Entity and Entity2.
    builder.applySetting(REGION_CACHE_PROPERTY + ENTITY2_NAME, ENTITY_NAME);
    StandardServiceRegistry srvcRegistry = builder.build();
    MetadataSources metadataSources = new MetadataSources(srvcRegistry);
    for (Class entityClass : getAnnotatedClasses()) metadataSources.addAnnotatedClass(entityClass);
    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (!entityBinding.isInherited())
            ((RootClass) entityBinding).setCacheConcurrencyStrategy(accessType.getExternalName());
    }
    for (org.hibernate.mapping.Collection collectionBinding : metadata.getCollectionBindings()) collectionBinding.setCacheConcurrencyStrategy(accessType.getExternalName());
    return metadata.buildSessionFactory();
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) Map(java.util.Map) HashMap(java.util.HashMap) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 93 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project ignite by apache.

the class HibernateL2CacheTransactionalSelfTest method registryBuilder.

/**
 * {@inheritDoc}
 */
@Nullable
@Override
protected StandardServiceRegistryBuilder registryBuilder() {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    DatasourceConnectionProviderImpl connProvider = new DatasourceConnectionProviderImpl();
    // JTA-aware data source.
    BasicManagedDataSource dataSrc = new BasicManagedDataSource();
    dataSrc.setTransactionManager(jotm.getTransactionManager());
    dataSrc.setDefaultAutoCommit(false);
    JdbcDataSource h2DataSrc = new JdbcDataSource();
    h2DataSrc.setURL(CONNECTION_URL);
    dataSrc.setXaDataSourceInstance(h2DataSrc);
    connProvider.setDataSource(dataSrc);
    connProvider.configure(Collections.emptyMap());
    builder.addService(ConnectionProvider.class, connProvider);
    builder.addService(JtaPlatform.class, new TestJtaPlatform());
    builder.applySetting(Environment.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class.getName());
    return builder;
}
Also used : JtaTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) DatasourceConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) BasicManagedDataSource(org.apache.commons.dbcp.managed.BasicManagedDataSource) Nullable(org.jetbrains.annotations.Nullable)

Example 94 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project ignite by apache.

the class HibernateL2CacheConfigurationSelfTest method startHibernate.

/**
 * @param igniteInstanceName Name of the grid providing caches.
 * @return Session factory.
 */
private SessionFactory startHibernate(String igniteInstanceName) {
    Configuration cfg = hibernateConfiguration(igniteInstanceName);
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    builder.applySetting("hibernate.connection.url", CONNECTION_URL);
    builder.applySetting("hibernate.show_sql", false);
    builder.applySettings(cfg.getProperties());
    return cfg.buildSessionFactory(builder.build());
}
Also used : Configuration(org.hibernate.cfg.Configuration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder)

Example 95 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class TimestampsRegionImplTest method testClearTimestampsRegionInIsolated.

public void testClearTimestampsRegionInIsolated() throws Exception {
    StandardServiceRegistryBuilder ssrb = createStandardServiceRegistryBuilder();
    final StandardServiceRegistry registry = ssrb.build();
    final StandardServiceRegistry registry2 = ssrb.build();
    try {
        final Properties properties = CacheTestUtil.toProperties(ssrb.getSettings());
        InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(registry, getCacheTestSupport());
        InfinispanRegionFactory regionFactory2 = CacheTestUtil.startRegionFactory(registry2, getCacheTestSupport());
        TimestampsRegionImpl region = (TimestampsRegionImpl) regionFactory.buildTimestampsRegion(getStandardRegionName(REGION_PREFIX), properties);
        TimestampsRegionImpl region2 = (TimestampsRegionImpl) regionFactory2.buildTimestampsRegion(getStandardRegionName(REGION_PREFIX), properties);
        Account acct = new Account();
        acct.setAccountHolder(new AccountHolder());
        region.getCache().withFlags(Flag.FORCE_SYNCHRONOUS).put(acct, "boo");
    } finally {
        StandardServiceRegistryBuilder.destroy(registry);
        StandardServiceRegistryBuilder.destroy(registry2);
    }
}
Also used : TimestampsRegionImpl(org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl) Account(org.hibernate.test.cache.infinispan.functional.entities.Account) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Properties(java.util.Properties) AccountHolder(org.hibernate.test.cache.infinispan.functional.entities.AccountHolder) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Aggregations

StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)167 MetadataSources (org.hibernate.boot.MetadataSources)106 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)94 Test (org.junit.Test)92 Metadata (org.hibernate.boot.Metadata)46 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)34 PersistentClass (org.hibernate.mapping.PersistentClass)27 Before (org.junit.Before)26 Properties (java.util.Properties)24 TestForIssue (org.hibernate.testing.TestForIssue)23 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)17 SessionFactory (org.hibernate.SessionFactory)15 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)14 Property (org.hibernate.mapping.Property)14 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)14 File (java.io.File)13 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)13 Map (java.util.Map)12 HashMap (java.util.HashMap)11 ServiceRegistry (org.hibernate.service.ServiceRegistry)11