org.hibernate.jpa.boot.internal.PersistenceXmlParser Java Examples

The following examples show how to use org.hibernate.jpa.boot.internal.PersistenceXmlParser. 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: JpaUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static EntityManagerFactory createEntityManagerFactory(KeycloakSession session, String unitName, Map<String, Object> properties, boolean jta) {
    PersistenceUnitTransactionType txType = jta ? PersistenceUnitTransactionType.JTA : PersistenceUnitTransactionType.RESOURCE_LOCAL;
    List<ParsedPersistenceXmlDescriptor> persistenceUnits = PersistenceXmlParser.locatePersistenceUnits(properties);
    for (ParsedPersistenceXmlDescriptor persistenceUnit : persistenceUnits) {
        if (persistenceUnit.getName().equals(unitName)) {
            List<Class<?>> providedEntities = getProvidedEntities(session);
            for (Class<?> entityClass : providedEntities) {
                // Add all extra entity classes to the persistence unit.
                persistenceUnit.addClasses(entityClass.getName());
            }
            // Now build the entity manager factory, supplying a proxy classloader, so Hibernate will be able
            // to find and load the extra provided entities.
            persistenceUnit.setTransactionType(txType);
            return Bootstrap.getEntityManagerFactoryBuilder(persistenceUnit, properties,
                    new ProxyClassLoader(providedEntities)).build();
        }
    }
    throw new RuntimeException("Persistence unit '" + unitName + "' not found");
}
 
Example #2
Source File: Bootstrap.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Intended for use in Hibernate tests
 *
 * @param persistenceXmlUrl The URL to a persistence.xml
 * @param persistenceUnitName The name of the persistence-unit to parse
 * @param integration setting overrides
 *
 * @return The EMFB
 */
public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
		URL persistenceXmlUrl,
		String persistenceUnitName,
		PersistenceUnitTransactionType transactionType,
		Map integration) {
	;
	return new EntityManagerFactoryBuilderImpl(
			PersistenceXmlParser.parse( persistenceXmlUrl, transactionType, integration ).get( persistenceUnitName ),
			integration
	);
}
 
Example #3
Source File: PersistenceXmlUtility.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link ParsedPersistenceXmlDescriptor} instance constructed from persistence.xml.
 */
public static ParsedPersistenceXmlDescriptor getParsedPersistenceXmlDescriptor() {
  return PersistenceXmlParser.locatePersistenceUnits(new Properties()).stream()
      .filter(unit -> PersistenceModule.PERSISTENCE_UNIT_NAME.equals(unit.getName()))
      .findFirst()
      .orElseThrow(
          () ->
              new IllegalArgumentException(
                  String.format(
                      "Could not find persistence unit with name %s",
                      PersistenceModule.PERSISTENCE_UNIT_NAME)));
}
 
Example #4
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 #5
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;
}