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

The following examples show how to use org.hibernate.jpa.boot.spi.Bootstrap. 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: BaseEntityManagerFunctionalTestCase.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * The entityManagerFactory in this method has been changed from original. It is now STATIC
 * because the teardown method annotated @AfterClass is now run just once and AfterClass methods
 * must be static. The entityManagerFactory is the only dependency of that method and it needed to
 * become a static single reference as a result.
 */
@Before
@SuppressWarnings({"UnusedDeclaration"})
public void buildEntityManagerFactory() {
  if (entityManagerFactory == null) {
    log.trace("Building EntityManagerFactory");

    entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
        buildPersistenceUnitDescriptor(),
        buildSettings()
    ).build().unwrap(SessionFactoryImplementor.class);

    serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getServiceRegistry()
        .getParentServiceRegistry();

    afterEntityManagerFactoryBuilt();
  }
  cleanTables();
}
 
Example #2
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 #3
Source File: JpaTransactionManagerRule.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** Constructs the {@link EntityManagerFactory} instance. */
private EntityManagerFactory createEntityManagerFactory(
    String jdbcUrl,
    String username,
    String password,
    ImmutableMap<String, String> configs,
    ImmutableList<Class> extraEntityClasses) {
  HashMap<String, String> properties = Maps.newHashMap(configs);
  properties.put(Environment.URL, jdbcUrl);
  properties.put(Environment.USER, username);
  properties.put(Environment.PASS, password);
  // Tell Postgresql JDBC driver to expect out-of-band schema change.
  properties.put("hibernate.hikari.dataSource.autosave", "conservative");

  ParsedPersistenceXmlDescriptor descriptor =
      PersistenceXmlUtility.getParsedPersistenceXmlDescriptor();

  // If we don't include the nomulus schema, remove all entity classes in the descriptor but keep
  // other settings like the converter classes.
  if (!includeNomulusSchema) {
    List<String> nonEntityClasses =
        descriptor.getManagedClassNames().stream()
            .filter(
                classString -> {
                  try {
                    return !Class.forName(classString).isAnnotationPresent(Entity.class);
                  } catch (ClassNotFoundException e) {
                    throw new IllegalArgumentException(e);
                  }
                })
            .collect(toImmutableList());
    descriptor.getManagedClassNames().clear();
    descriptor.getManagedClassNames().addAll(nonEntityClasses);
  }

  extraEntityClasses.stream().map(Class::getName).forEach(descriptor::addClasses);
  return Bootstrap.getEntityManagerFactoryBuilder(descriptor, properties).build();
}
 
Example #4
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 #5
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 #6
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 );
}