下面列出了怎么用org.hibernate.jpa.boot.spi.Bootstrap的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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();
}
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");
}
/** 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();
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitInfo info, Map integration) {
return Bootstrap.getEntityManagerFactoryBuilder( info, integration );
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitDescriptor persistenceUnitDescriptor,
Map integration, ClassLoader providedClassLoader) {
return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnitDescriptor, integration, providedClassLoader );
}
protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(PersistenceUnitDescriptor persistenceUnitDescriptor,
Map integration, ClassLoaderService providedClassLoaderService) {
return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnitDescriptor, integration, providedClassLoaderService );
}