public SchemaExport(MetadataImplementor metadata) { ServiceRegistry serviceRegistry = metadata.getServiceRegistry(); this.connectionHelper = new SuppliedConnectionProviderConnectionHelper( serviceRegistry.getService( ConnectionProvider.class ) ); JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class ); this.sqlStatementLogger = jdbcServices.getSqlStatementLogger(); this.formatter = ( sqlStatementLogger.isFormat() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter(); this.sqlExceptionHelper = jdbcServices.getSqlExceptionHelper(); this.importFiles = ConfigurationHelper.getString( AvailableSettings.HBM2DDL_IMPORT_FILES, serviceRegistry.getService( ConfigurationService.class ).getSettings(), DEFAULT_IMPORT_FILE ); final Dialect dialect = jdbcServices.getDialect(); this.dropSQL = metadata.getDatabase().generateDropSchemaScript( dialect ); this.createSQL = metadata.getDatabase().generateSchemaCreationScript( dialect ); }
private EntityCopyObserver createEntityCopyObserver(SessionFactoryImplementor sessionFactory) { final ServiceRegistry serviceRegistry = sessionFactory.getServiceRegistry(); if ( entityCopyObserverStrategy == null ) { final ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class ); entityCopyObserverStrategy = configurationService.getSetting( "hibernate.event.merge.entity_copy_observer", new ConfigurationService.Converter<String>() { @Override public String convert(Object value) { return value.toString(); } }, EntityCopyNotAllowedObserver.SHORT_NAME ); LOG.debugf( "EntityCopyObserver strategy: %s", entityCopyObserverStrategy ); } final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class ); return strategySelector.resolveStrategy( EntityCopyObserver.class, entityCopyObserverStrategy ); }
@Test public void testCreationOfSessionFactory() { Properties props = getProperties(); Config conf = ConfigFactory.parseProperties(props); hs = new HibernateService(conf); SessionFactory sf = hs.getSessionFactory(); assertNotNull(sf); assertFalse(sf.isClosed()); // traverse through the session factory to get at configuration values SessionFactoryOptions sfo = sf.getSessionFactoryOptions(); StandardServiceRegistry ssr = sfo.getServiceRegistry(); ConfigurationService cs = ssr.getService(ConfigurationService.class); assertEquals(props.getProperty("hibernate.connection.driver_class"), cs.getSetting("hibernate.connection.driver_class", StandardConverters.STRING)); assertEquals(props.getProperty("hibernate.connection.url"), cs.getSetting("hibernate.connection.url", StandardConverters.STRING)); assertEquals(props.getProperty("hibernate.dialect"), cs.getSetting("hibernate.dialect", StandardConverters.STRING)); assertEquals(props.getProperty("hibernate.hbm2ddl.auto"), cs.getSetting("hibernate.hbm2ddl.auto", StandardConverters.STRING)); // check mapping ClassMetadata cm = sf.getClassMetadata(TestObject.class); String[] names = cm.getPropertyNames(); assertEquals(1, names.length); assertEquals("name", names[0]); assertEquals("string", cm.getPropertyType("name").getName()); }
@Override public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) { ConfigurationService config = metadata.getDatabase().getBuildingOptions().getServiceRegistry() .getService(ConfigurationService.class); if (config != null) { String value = config.getSetting("hibernate.hbm2ddl.auto", StandardConverters.STRING); if (!value.equalsIgnoreCase("update")) { // We should only check whether it is already present in an // update scenario, in all other scenarios, just return the // actual create statement. return org.hibernate.mapping.Index.buildSqlCreateIndexString(dialect, uniqueKey.getName(), uniqueKey.getTable(), uniqueKey.columnIterator(), uniqueKey.getColumnOrderMap(), true, metadata); } } // First check that this unique key is not already present, as this is a // lot faster than trying to create it and then fail. initIndices(); UniqueIndex idx = indices.getIndex(uniqueKey); if (idx != null) { return null; } return org.hibernate.mapping.Index.buildSqlCreateIndexString(dialect, uniqueKey.getName(), uniqueKey.getTable(), uniqueKey.columnIterator(), uniqueKey.getColumnOrderMap(), true, metadata); }
@SuppressWarnings( {"unchecked"} ) void resolve() { for ( EntityBinding entityBinding : metadata.getEntityBindings() ) { if ( entityBinding.isRoot() ) { Properties properties = new Properties( ); properties.putAll( metadata.getServiceRegistry() .getService( ConfigurationService.class ) .getSettings() ); //TODO: where should these be added??? if ( ! properties.contains( AvailableSettings.PREFER_POOLED_VALUES_LO ) ) { properties.put( AvailableSettings.PREFER_POOLED_VALUES_LO, "false" ); } if ( ! properties.contains( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER ) ) { properties.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizerImpl( metadata ) ); } entityBinding.getHierarchyDetails().getEntityIdentifier().createIdentifierGenerator( metadata.getIdentifierGeneratorFactory(), properties ); } } }
@Override public StatisticsImplementor initiateService( SessionFactoryImplementor sessionFactory, MetadataImplementor metadata, ServiceRegistryImplementor registry) { ConfigurationService configurationService = registry.getService( ConfigurationService.class ); final Object configValue = configurationService.getSetting( STATS_BUILDER, null ); return initiateServiceInternal( sessionFactory, configValue, registry ); }
@SuppressWarnings({ "unchecked" }) private CurrentTenantIdentifierResolver determineCurrentTenantIdentifierResolver( CurrentTenantIdentifierResolver explicitResolver) { if ( explicitResolver != null ) { return explicitResolver; } return serviceRegistry.getService( ConfigurationService.class ) .getSetting( AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, CurrentTenantIdentifierResolver.class, null ); }
@Override public void injectServices(ServiceRegistryImplementor serviceRegistry) { Map settings = serviceRegistry.getService(ConfigurationService.class).getSettings(); DataSource dataSource = (DataSource) settings.get(Environment.DATASOURCE); datasourceConnectionProvider.setDataSource(dataSource); datasourceConnectionProvider.configure(settings); logger.debug("connection provider:{}", datasourceConnectionProvider); }
@Override public void prepare(JdbcServices jdbcServices, JdbcConnectionAccess connectionAccess, MetadataImplementor metadata, SessionFactoryOptions sessionFactoryOptions) { ConfigurationService configService = sessionFactoryOptions.getServiceRegistry().getService(ConfigurationService.class); this.fullyQualifiedTableName = Objects.requireNonNull(configService.getSetting(TABLE, String.class, null), "Property " + TABLE + " must be set."); this.idColumn = configService.getSetting(ID_COLUMN, String.class, "ID"); this.discriminatorColumn = configService.getSetting(DISCRIMINATOR_COLUMN, String.class, "ENTITY_NAME"); this.cleanRows = configService.getSetting(CLEAN_ROWS, StandardConverters.BOOLEAN, false); }
@Override public void configure( Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class); final Dialect dialect = jdbcEnvironment.getDialect(); final ConfigurationService configurationService = serviceRegistry.getService(ConfigurationService.class); String globalEntityIdentifierPrefix = configurationService.getSetting( "entity.identifier.prefix", String.class, "SEQ_" ); sequencePrefix = ConfigurationHelper.getString( SEQUENCE_PREFIX, params, globalEntityIdentifierPrefix); final String sequencePerEntitySuffix = ConfigurationHelper.getString( SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params, SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX); final String defaultSequenceName = ConfigurationHelper.getBoolean( SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, params, false) ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix : SequenceStyleGenerator.DEF_SEQUENCE_NAME; sequenceCallSyntax = dialect.getSequenceNextValString( ConfigurationHelper.getString( SequenceStyleGenerator.SEQUENCE_PARAM, params, defaultSequenceName)); }
@Override public void injectServices(ServiceRegistryImplementor serviceRegistry) { final Object dataSourceConfigValue = serviceRegistry.getService( ConfigurationService.class ) .getSettings() .get( AvailableSettings.DATASOURCE ); if ( dataSourceConfigValue == null || ! String.class.isInstance( dataSourceConfigValue ) ) { throw new HibernateException( "Improper set up of DataSourceBasedMultiTenantConnectionProviderImpl" ); } final String jndiName = (String) dataSourceConfigValue; jndiService = serviceRegistry.getService( JndiService.class ); if ( jndiService == null ) { throw new HibernateException( "Could not locate JndiService from DataSourceBasedMultiTenantConnectionProviderImpl" ); } final Object namedObject = jndiService.locate( jndiName ); if ( namedObject == null ) { throw new HibernateException( "JNDI name [" + jndiName + "] could not be resolved" ); } if ( DataSource.class.isInstance( namedObject ) ) { final int loc = jndiName.lastIndexOf( "/" ); this.baseJndiNamespace = jndiName.substring( 0, loc ); this.tenantIdentifierForAny = jndiName.substring( loc + 1 ); dataSourceMap().put( tenantIdentifierForAny, (DataSource) namedObject ); } else if ( Context.class.isInstance( namedObject ) ) { this.baseJndiNamespace = jndiName; this.tenantIdentifierForAny = (String) serviceRegistry.getService( ConfigurationService.class ) .getSettings() .get( TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY ); if ( tenantIdentifierForAny == null ) { throw new HibernateException( "JNDI name named a Context, but tenant identifier to use for ANY was not specified" ); } } else { throw new HibernateException( "Unknown object type [" + namedObject.getClass().getName() + "] found in JNDI location [" + jndiName + "]" ); } }
@Override public ConfigurationService initiateService(Map configurationValues, ServiceRegistryImplementor registry) { return new ConfigurationServiceImpl( configurationValues ); }
@Override public Class<ConfigurationService> getServiceInitiated() { return ConfigurationService.class; }
@Override public void injectServices(ServiceRegistryImplementor serviceRegistry) { Map lSettings = serviceRegistry.getService(ConfigurationService.class).getSettings(); lazyDatasource = (DataSource) lSettings.get(Environment.DATASOURCE); }