Search in sources :

Example 1 with JpaConfiguration

use of io.micronaut.configuration.hibernate.jpa.JpaConfiguration in project micronaut-sql by micronaut-projects.

the class EntitiesInPackageCondition method matches.

@Override
public boolean matches(ConditionContext context) {
    final AnnotationMetadataProvider component = context.getComponent();
    if (component instanceof BeanDefinition) {
        BeanDefinition<?> definition = (BeanDefinition<?>) component;
        final BeanContext beanContext = context.getBeanContext();
        if (beanContext instanceof ApplicationContext) {
            final Optional<String> name = definition instanceof NameResolver ? ((NameResolver) definition).resolveName() : Optional.empty();
            final Qualifier<JpaConfiguration> q = Qualifiers.byName(name.orElse("default"));
            final Optional<JpaConfiguration> jpaConfiguration = beanContext.findBean(JpaConfiguration.class, q);
            JpaConfiguration.EntityScanConfiguration entityScanConfig = jpaConfiguration.map(JpaConfiguration::getEntityScanConfiguration).orElse(null);
            if (entityScanConfig == null) {
                return false;
            }
            boolean isClasspathScanEnabled = entityScanConfig.isClasspath();
            String[] packagesToScan = entityScanConfig.getPackages();
            boolean hasEntitiesOnClassPath = false;
            boolean hasIntrospections = false;
            if (isClasspathScanEnabled) {
                final Environment environment = ((ApplicationContext) beanContext).getEnvironment();
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasEntitiesOnClassPath = environment.scan(Entity.class, packagesToScan).findAny().isPresent();
                } else {
                    hasEntitiesOnClassPath = environment.scan(Entity.class).findAny().isPresent();
                }
            } else {
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasIntrospections = !BeanIntrospector.SHARED.findIntrospections(Entity.class, packagesToScan).isEmpty();
                } else {
                    hasIntrospections = !BeanIntrospector.SHARED.findIntrospections(Entity.class).isEmpty();
                }
            }
            return hasEntitiesOnClassPath || hasIntrospections;
        }
    }
    return true;
}
Also used : Entity(javax.persistence.Entity) BeanDefinition(io.micronaut.inject.BeanDefinition) BeanContext(io.micronaut.context.BeanContext) ApplicationContext(io.micronaut.context.ApplicationContext) Environment(io.micronaut.context.env.Environment) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) NameResolver(io.micronaut.core.naming.NameResolver) JpaConfiguration(io.micronaut.configuration.hibernate.jpa.JpaConfiguration)

Example 2 with JpaConfiguration

use of io.micronaut.configuration.hibernate.jpa.JpaConfiguration in project micronaut-sql by micronaut-projects.

the class HibernateJCacheManagerBinder method onCreated.

@Override
public JpaConfiguration onCreated(BeanCreatedEvent<JpaConfiguration> event) {
    JpaConfiguration jpaConfiguration = event.getBean();
    Map<String, Object> jpaProperties = jpaConfiguration.getProperties();
    jpaProperties.put(ConfigSettings.CACHE_MANAGER, cacheManager);
    return jpaConfiguration;
}
Also used : JpaConfiguration(io.micronaut.configuration.hibernate.jpa.JpaConfiguration)

Example 3 with JpaConfiguration

use of io.micronaut.configuration.hibernate.jpa.JpaConfiguration in project mintaka by FIWARE.

the class MultiTenantEntityManagerFactoryBean method hibernateStandardServiceRegistry.

@EachBean(DataSource.class)
@Replaces(factory = EntityManagerFactoryBean.class, bean = StandardServiceRegistry.class)
protected StandardServiceRegistry hibernateStandardServiceRegistry(@Parameter String dataSourceName, DataSource dataSource) {
    if (dataSource instanceof DelegatingDataSource) {
        dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
    }
    connectionProvider.registerDefaultDatasource(dataSource);
    Map<String, Object> additionalSettings = new LinkedHashMap<>();
    additionalSettings.put("hibernate.connection.datasource", dataSource);
    additionalSettings.put("hibernate.current_session_context_class", MicronautSessionContext.class.getName());
    additionalSettings.put("hibernate.session_factory_name", dataSourceName);
    additionalSettings.put("hibernate.session_factory_name_is_jndi", false);
    additionalSettings.putIfAbsent(AvailableSettings.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
    additionalSettings.putIfAbsent(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, connectionProvider);
    additionalSettings.putIfAbsent(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, multiTenantResolver);
    JpaConfiguration currentJpaConfiguration = this.beanLocator.findBean(JpaConfiguration.class, Qualifiers.byName(dataSourceName)).orElse(this.jpaConfiguration);
    return currentJpaConfiguration.buildStandardServiceRegistry(additionalSettings);
}
Also used : DelegatingDataSource(io.micronaut.transaction.jdbc.DelegatingDataSource) LinkedHashMap(java.util.LinkedHashMap) MicronautSessionContext(io.micronaut.transaction.hibernate5.MicronautSessionContext) JpaConfiguration(io.micronaut.configuration.hibernate.jpa.JpaConfiguration) EachBean(io.micronaut.context.annotation.EachBean) Replaces(io.micronaut.context.annotation.Replaces)

Example 4 with JpaConfiguration

use of io.micronaut.configuration.hibernate.jpa.JpaConfiguration in project micronaut-data by micronaut-projects.

the class PhysicalNamingStrategyConfiguration method onCreated.

@Override
public JpaConfiguration onCreated(BeanCreatedEvent<JpaConfiguration> event) {
    JpaConfiguration jpaConfiguration = event.getBean();
    jpaConfiguration.getProperties().putIfAbsent(AvailableSettings.PHYSICAL_NAMING_STRATEGY, physicalNamingStrategy);
    return jpaConfiguration;
}
Also used : JpaConfiguration(io.micronaut.configuration.hibernate.jpa.JpaConfiguration)

Aggregations

JpaConfiguration (io.micronaut.configuration.hibernate.jpa.JpaConfiguration)4 ApplicationContext (io.micronaut.context.ApplicationContext)1 BeanContext (io.micronaut.context.BeanContext)1 EachBean (io.micronaut.context.annotation.EachBean)1 Replaces (io.micronaut.context.annotation.Replaces)1 Environment (io.micronaut.context.env.Environment)1 AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)1 NameResolver (io.micronaut.core.naming.NameResolver)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 MicronautSessionContext (io.micronaut.transaction.hibernate5.MicronautSessionContext)1 DelegatingDataSource (io.micronaut.transaction.jdbc.DelegatingDataSource)1 LinkedHashMap (java.util.LinkedHashMap)1 Entity (javax.persistence.Entity)1