org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder Java Examples

The following examples show how to use org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastBootHibernateReactivePersistenceProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public EntityManagerFactory createEntityManagerFactory(String emName, Map properties) {
    if (properties == null)
        properties = new HashMap<Object, Object>();
    try {
        // These are pre-parsed during image generation:
        final List<PersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

        for (PersistenceUnitDescriptor unit : units) {
            //if the provider is not set, don't use it as people might want to use Hibernate ORM
            if (IMPLEMENTATION_NAME.equalsIgnoreCase(unit.getProviderClassName()) ||
                    unit.getProviderClassName() == null) {
                EntityManagerFactoryBuilder emfBuilder = getEntityManagerFactoryBuilderOrNull(emName, properties);
                EntityManagerFactory emf = emfBuilder.build();
                return emf;
            }
        }

        //not the right provider
        return null;
    } catch (PersistenceException pe) {
        throw pe;
    } catch (Exception e) {
        throw new PersistenceException("Unable to build EntityManagerFactory", e);
    }
}
 
Example #2
Source File: CustomHibernatePersistenceProvider.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public EntityManagerFactory createEMF(Map properties) {
	PersistenceUnitInfo info = new RapidoidPersistenceUnitInfo(persistenceUnitName, dataSource, classLoader);
	PersistenceUnitInfoDescriptor infoDescriptor = new PersistenceUnitInfoDescriptor(info);

	CustomDescriptor customDescriptor = new CustomDescriptor(infoDescriptor, entityTypes);
	EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilder(customDescriptor, properties, classLoader);

	return builder.build();
}
 
Example #3
Source File: ReactivePersistenceProvider.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p/>
 * Note: per-spec, the values passed as {@code properties} override values found in {@code persistence.xml}
 */
@Override
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
	log.tracef( "Starting createEntityManagerFactory for persistenceUnitName %s", persistenceUnitName );
	final Map immutableProperties = immutable( properties );
	final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, immutableProperties );
	if ( builder == null ) {
		log.trace( "Could not obtain matching EntityManagerFactoryBuilder, returning null" );
		return null;
	}
	else {
		return builder.build();
	}
}
 
Example #4
Source File: EntityManagerFactoryBuilderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory) {
	this.validatorFactory = validatorFactory;

	if ( validatorFactory != null ) {
		BeanValidationIntegrator.validateFactory( validatorFactory );
	}
	return this;
}
 
Example #5
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean generateSchema(String persistenceUnitName, Map map) {
	log.tracef( "Starting generateSchema for persistenceUnitName %s", persistenceUnitName );

	final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, map );
	if ( builder == null ) {
		log.trace( "Could not obtain matching EntityManagerFactoryBuilder, returning false" );
		return false;
	}
	builder.generateSchema();
	return true;
}
 
Example #6
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateSchema(PersistenceUnitInfo info, Map map) {
	log.tracef( "Starting generateSchema : PUI.name=%s", info.getPersistenceUnitName() );

	final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilder( info, map );
	builder.generateSchema();
}
 
Example #7
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p/>
 * Note: per-spec, the values passed as {@code properties} override values found in {@code persistence.xml}
 */
@Override
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
	log.tracef( "Starting createEntityManagerFactory for persistenceUnitName %s", persistenceUnitName );
	final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties );
	if ( builder == null ) {
		log.trace( "Could not obtain matching EntityManagerFactoryBuilder, returning null" );
		return null;
	}
	else {
		return builder.build();
	}
}
 
Example #8
Source File: FastBootHibernatePersistenceProvider.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public boolean generateSchema(String persistenceUnitName, Map map) {
    log.tracef("Starting generateSchema for persistenceUnitName %s", persistenceUnitName);

    final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull(persistenceUnitName, map);
    if (builder == null) {
        log.trace("Could not obtain matching EntityManagerFactoryBuilder, returning false");
        return false;
    }
    builder.generateSchema();
    return true;
}
 
Example #9
Source File: FastBootHibernatePersistenceProvider.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public void generateSchema(PersistenceUnitInfo info, Map map) {
    log.tracef("Starting generateSchema : PUI.name=%s", info.getPersistenceUnitName());

    final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilder(info, map);
    builder.generateSchema();
}
 
Example #10
Source File: ReactivePersistenceProvider.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void generateSchema(PersistenceUnitInfo info, Map map) {
	log.tracef( "Starting generateSchema : PUI.name=%s", info.getPersistenceUnitName() );

	final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilder( info, map );
	builder.generateSchema();
}
 
Example #11
Source File: ReactivePersistenceProvider.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public boolean generateSchema(String persistenceUnitName, Map map) {
	log.tracef( "Starting generateSchema for persistenceUnitName %s", persistenceUnitName );

	final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, map );
	if ( builder == null ) {
		log.trace( "Could not obtain matching EntityManagerFactoryBuilder, returning false" );
		return false;
	}
	builder.generateSchema();
	return true;
}
 
Example #12
Source File: FastBootHibernatePersistenceProvider.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
    log.tracef("Starting createEntityManagerFactory for persistenceUnitName %s", persistenceUnitName);
    final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull(persistenceUnitName,
            properties);
    if (builder == null) {
        log.trace("Could not obtain matching EntityManagerFactoryBuilder, returning null");
        return null;
    } else {
        return builder.build();
    }
}
 
Example #13
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties,
		ClassLoaderService providedClassLoaderService) {
	return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, null, providedClassLoaderService );
}
 
Example #14
Source File: ReactivePersistenceProvider.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties) {
	log.tracef( "Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s", persistenceUnitName );

	final List<ParsedPersistenceXmlDescriptor> units;
	try {
		units = PersistenceXmlParser.locatePersistenceUnits( properties );
	}
	catch (Exception e) {
		log.debug( "Unable to locate persistence units", e );
		throw new PersistenceException( "Unable to locate persistence units", e );
	}

	log.debugf( "Located and parsed %s persistence units; checking each", units.size() );

	if ( persistenceUnitName == null && units.size() > 1 ) {
		// no persistence-unit name to look for was given and we found multiple persistence-units
		throw new PersistenceException( "No name provided and multiple persistence units found" );
	}

	for ( ParsedPersistenceXmlDescriptor persistenceUnit : units ) {
		log.debugf(
				"Checking persistence-unit [name=%s, explicit-provider=%s] against incoming persistence unit name [%s]",
				persistenceUnit.getName(),
				persistenceUnit.getProviderClassName(),
				persistenceUnitName
		);

		final boolean matches = persistenceUnitName == null || persistenceUnit.getName().equals( persistenceUnitName );
		if ( !matches ) {
			log.debug( "Excluding from consideration due to name mis-match" );
			continue;
		}

		// See if we (Hibernate Reactive) are the persistence provider
		if ( ! ReactiveProviderChecker.isProvider( persistenceUnit, properties ) ) {
			log.debug( "Excluding from consideration due to provider mis-match" );
			continue;
		}

		return getEntityManagerFactoryBuilder( persistenceUnit, properties );
	}

	log.debug( "Found no matching persistence units" );
	return null;
}
 
Example #15
Source File: EntityManagerFactoryBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public EntityManagerFactoryBuilder withDataSource(DataSource dataSource) {
	this.dataSource = dataSource;

	return this;
}
 
Example #16
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitDescriptor persistenceUnitDescriptor,
		Map integration, ClassLoaderService providedClassLoaderService) {
	return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnitDescriptor, integration, providedClassLoaderService );
}
 
Example #17
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitDescriptor persistenceUnitDescriptor,
		Map integration, ClassLoader providedClassLoader) {
	return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnitDescriptor, integration, providedClassLoader );
}
 
Example #18
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitInfo info, Map integration) {
	return Bootstrap.getEntityManagerFactoryBuilder( info, integration );
}
 
Example #19
Source File: ReactivePersistenceProvider.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitInfo info, Map integration) {
	return getEntityManagerFactoryBuilder( new PersistenceUnitInfoDescriptor( info ), integration );
}
 
Example #20
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties,
		ClassLoader providedClassLoader, ClassLoaderService providedClassLoaderService) {
	log.tracef( "Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s", persistenceUnitName );

	final Map integration = wrap( properties );
	final List<ParsedPersistenceXmlDescriptor> units;
	try {
		units = PersistenceXmlParser.locatePersistenceUnits( integration );
	}
	catch (Exception e) {
		log.debug( "Unable to locate persistence units", e );
		throw new PersistenceException( "Unable to locate persistence units", e );
	}

	log.debugf( "Located and parsed %s persistence units; checking each", units.size() );

	if ( persistenceUnitName == null && units.size() > 1 ) {
		// no persistence-unit name to look for was given and we found multiple persistence-units
		throw new PersistenceException( "No name provided and multiple persistence units found" );
	}

	for ( ParsedPersistenceXmlDescriptor persistenceUnit : units ) {
		log.debugf(
				"Checking persistence-unit [name=%s, explicit-provider=%s] against incoming persistence unit name [%s]",
				persistenceUnit.getName(),
				persistenceUnit.getProviderClassName(),
				persistenceUnitName
		);

		final boolean matches = persistenceUnitName == null || persistenceUnit.getName().equals( persistenceUnitName );
		if ( !matches ) {
			log.debug( "Excluding from consideration due to name mis-match" );
			continue;
		}

		// See if we (Hibernate) are the persistence provider
		if ( ! ProviderChecker.isProvider( persistenceUnit, properties ) ) {
			log.debug( "Excluding from consideration due to provider mis-match" );
			continue;
		}

		if (providedClassLoaderService != null) {
			return getEntityManagerFactoryBuilder( persistenceUnit, integration, providedClassLoaderService );
		}
		else {
			return getEntityManagerFactoryBuilder( persistenceUnit, integration, providedClassLoader );
		}
	}

	log.debug( "Found no matching persistence units" );
	return null;
}
 
Example #21
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties,
		ClassLoader providedClassLoader) {
	return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, providedClassLoader, null );
}
 
Example #22
Source File: HibernatePersistenceProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties) {
	return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, null, null );
}
 
Example #23
Source File: ReactivePersistenceProvider.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitDescriptor persistenceUnitDescriptor, Map integration) {
	return new ReactiveEntityManagerFactoryBuilder( persistenceUnitDescriptor, integration );
}
 
Example #24
Source File: FastBootHibernateReactivePersistenceProvider.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName,
        Map properties) {
    log.tracef("Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s",
            persistenceUnitName);

    verifyProperties(properties);

    // These are pre-parsed during image generation:
    final List<PersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

    log.debugf("Located %s persistence units; checking each", units.size());

    if (persistenceUnitName == null && units.size() > 1) {
        // no persistence-unit name to look for was given and we found multiple
        // persistence-units
        throw new PersistenceException("No name provided and multiple persistence units found");
    }

    for (PersistenceUnitDescriptor persistenceUnit : units) {
        log.debugf(
                "Checking persistence-unit [name=%s, explicit-provider=%s] against incoming persistence unit name [%s]",
                persistenceUnit.getName(), persistenceUnit.getProviderClassName(), persistenceUnitName);

        final boolean matches = persistenceUnitName == null
                || persistenceUnit.getName().equals(persistenceUnitName);
        if (!matches) {
            log.debugf("Excluding from consideration '%s' due to name mis-match", persistenceUnit.getName());
            continue;
        }

        // See if we (Hibernate) are the persistence provider
        if (!isProvider(persistenceUnit)) {
            log.debug("Excluding from consideration due to provider mis-match");
            continue;
        }

        RecordedState recordedState = PersistenceUnitsHolder.getRecordedState(persistenceUnitName);

        final PrevalidatedQuarkusMetadata metadata = recordedState.getMetadata();
        final BuildTimeSettings buildTimeSettings = recordedState.getBuildTimeSettings();
        final IntegrationSettings integrationSettings = recordedState.getIntegrationSettings();
        RuntimeSettings.Builder runtimeSettingsBuilder = new RuntimeSettings.Builder(buildTimeSettings,
                integrationSettings);

        HibernateOrmIntegrations.contributeRuntimeProperties((k, v) -> runtimeSettingsBuilder.put(k, v));

        RuntimeSettings runtimeSettings = runtimeSettingsBuilder.build();

        StandardServiceRegistry standardServiceRegistry = rewireMetadataAndExtractServiceRegistry(
                runtimeSettings, recordedState, persistenceUnitName);

        final Object cdiBeanManager = Arc.container().beanManager();
        final Object validatorFactory = Arc.container().instance("quarkus-hibernate-validator-factory").get();

        return new FastBootReactiveEntityManagerFactoryBuilder(
                metadata /* Uses the StandardServiceRegistry references by this! */,
                persistenceUnitName,
                standardServiceRegistry /* Mostly ignored! (yet needs to match) */,
                runtimeSettings,
                validatorFactory, cdiBeanManager, recordedState.getMultiTenancyStrategy());
    }

    log.debug("Found no matching persistence units");
    return null;
}
 
Example #25
Source File: FastBootEntityManagerFactoryBuilder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory) {
    return null;
}
 
Example #26
Source File: FastBootHibernatePersistenceProvider.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 * Copied and modified from
 * HibernatePersistenceProvider#getEntityManagerFactoryBuilderOrNull(String,
 * Map, ClassLoader, ClassLoaderService) Notable changes: - ignore the
 * ClassLoaderService parameter to inject our own custom implementation instead
 * - verify the Map properties are not set (or fail as we can't support runtime
 * overrides) - don't try looking for ParsedPersistenceXmlDescriptor resources
 * to parse, just take the pre-parsed ones from the static final field - class
 * annotations metadata is also injected
 */
@SuppressWarnings("rawtypes")
private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName,
        Map properties) {
    log.tracef("Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s",
            persistenceUnitName);

    verifyProperties(properties);

    // These are pre-parsed during image generation:
    final List<PersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

    log.debugf("Located %s persistence units; checking each", units.size());

    if (persistenceUnitName == null && units.size() > 1) {
        // no persistence-unit name to look for was given and we found multiple
        // persistence-units
        throw new PersistenceException("No name provided and multiple persistence units found");
    }

    for (PersistenceUnitDescriptor persistenceUnit : units) {
        log.debugf(
                "Checking persistence-unit [name=%s, explicit-provider=%s] against incoming persistence unit name [%s]",
                persistenceUnit.getName(), persistenceUnit.getProviderClassName(), persistenceUnitName);

        final boolean matches = persistenceUnitName == null
                || persistenceUnit.getName().equals(persistenceUnitName);
        if (!matches) {
            log.debugf("Excluding from consideration '%s' due to name mis-match", persistenceUnit.getName());
            continue;
        }

        // See if we (Hibernate) are the persistence provider
        if (!isProvider(persistenceUnit)) {
            log.debug("Excluding from consideration due to provider mis-match");
            continue;
        }

        RecordedState recordedState = PersistenceUnitsHolder.getRecordedState(persistenceUnitName);

        final PrevalidatedQuarkusMetadata metadata = recordedState.getMetadata();
        final BuildTimeSettings buildTimeSettings = recordedState.getBuildTimeSettings();
        final IntegrationSettings integrationSettings = recordedState.getIntegrationSettings();
        RuntimeSettings.Builder runtimeSettingsBuilder = new RuntimeSettings.Builder(buildTimeSettings,
                integrationSettings);

        // Inject the datasource
        injectDataSource(persistenceUnitName, runtimeSettingsBuilder);

        HibernateOrmIntegrations.contributeRuntimeProperties((k, v) -> runtimeSettingsBuilder.put(k, v));

        RuntimeSettings runtimeSettings = runtimeSettingsBuilder.build();

        StandardServiceRegistry standardServiceRegistry = rewireMetadataAndExtractServiceRegistry(
                runtimeSettings, recordedState);

        final Object cdiBeanManager = Arc.container().beanManager();
        final Object validatorFactory = Arc.container().instance("quarkus-hibernate-validator-factory").get();

        return new FastBootEntityManagerFactoryBuilder(
                metadata /* Uses the StandardServiceRegistry references by this! */,
                persistenceUnitName,
                standardServiceRegistry /* Mostly ignored! (yet needs to match) */,
                runtimeSettings,
                validatorFactory, cdiBeanManager, recordedState.getMultiTenancyStrategy());
    }

    log.debug("Found no matching persistence units");
    return null;
}
 
Example #27
Source File: FastBootHibernatePersistenceProvider.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(String persistenceUnitName, Map integration) {
    return getEntityManagerFactoryBuilderOrNull(persistenceUnitName, integration);
}
 
Example #28
Source File: FastBootHibernatePersistenceProvider.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitInfo info, Map integration) {
    return getEntityManagerFactoryBuilder(info.getPersistenceUnitName(), integration);
}
 
Example #29
Source File: FastBootEntityManagerFactoryBuilder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public EntityManagerFactoryBuilder withDataSource(DataSource dataSource) {
    return null;
}