@Test public void testContainerBootstrapSharedCacheMode() { // first, via the integration vars PersistenceUnitInfoAdapter empty = new PersistenceUnitInfoAdapter(); { // as object EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder( empty, Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE)); assertEquals( SharedCacheMode.DISABLE_SELECTIVE, builder.getConfigurationValues().get(AvailableSettings.SHARED_CACHE_MODE)); } { // as string EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder( empty, Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE.name())); assertEquals( SharedCacheMode.DISABLE_SELECTIVE.name(), builder.getConfigurationValues().get(AvailableSettings.SHARED_CACHE_MODE)); } // next, via the PUI PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter() { @Override public SharedCacheMode getSharedCacheMode() { return SharedCacheMode.ENABLE_SELECTIVE; } }; { EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(adapter, null); assertEquals( SharedCacheMode.ENABLE_SELECTIVE, builder.getConfigurationValues().get(AvailableSettings.SHARED_CACHE_MODE)); } // via both, integration vars should take precedence { EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder( adapter, Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE)); assertEquals( SharedCacheMode.DISABLE_SELECTIVE, builder.getConfigurationValues().get(AvailableSettings.SHARED_CACHE_MODE)); } }
@Test public void testContainerBootstrapValidationMode() { // first, via the integration vars PersistenceUnitInfoAdapter empty = new PersistenceUnitInfoAdapter(); { // as object EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder( empty, Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.CALLBACK)); assertEquals( ValidationMode.CALLBACK, builder.getConfigurationValues().get(AvailableSettings.VALIDATION_MODE)); } { // as string EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder( empty, Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.CALLBACK.name())); assertEquals( ValidationMode.CALLBACK.name(), builder.getConfigurationValues().get(AvailableSettings.VALIDATION_MODE)); } // next, via the PUI PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter() { @Override public ValidationMode getValidationMode() { return ValidationMode.CALLBACK; } }; { EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(adapter, null); assertEquals( ValidationMode.CALLBACK, builder.getConfigurationValues().get(AvailableSettings.VALIDATION_MODE)); } // via both, integration vars should take precedence { EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder( adapter, Collections.singletonMap(AvailableSettings.VALIDATION_MODE, ValidationMode.NONE)); assertEquals( ValidationMode.NONE, builder.getConfigurationValues().get(AvailableSettings.VALIDATION_MODE)); } }
@Test public void testCreateThenDrop() throws Exception { URL persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource(PERSISTENCE_XML_RESOURCE_NAME); if (persistenceXmlUrl == null) { persistenceXmlUrl = Thread.currentThread() .getContextClassLoader() .getResource('/' + PERSISTENCE_XML_RESOURCE_NAME); } assertNotNull(persistenceXmlUrl); ParsedPersistenceXmlDescriptor persistenceUnit = PersistenceXmlParser.locateIndividualPersistenceUnit(persistenceXmlUrl); // creating the EMF causes SchemaCreator to be run... EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(persistenceUnit, Collections.emptyMap()).build(); // closing the EMF causes the delayed SchemaDropper to be run... // wrap in a transaction just to see if we can get this to fail in the way the WF report says; // in my experience however this succeeds with or without the transaction final TransactionManager tm = emf.unwrap(SessionFactoryImplementor.class) .getServiceRegistry() .getService(JtaPlatform.class) .retrieveTransactionManager(); tm.begin(); Transaction txn = tm.getTransaction(); emf.close(); txn.commit(); }
@Override public void onApplicationEvent(ContextRefreshedEvent event) { String ddlAuto = (String) this.map.get("hibernate.hbm2ddl.auto"); if (ddlAuto == null || "none".equals(ddlAuto)) { return; } Bootstrap.getEntityManagerFactoryBuilder(this.factory.getPersistenceUnitInfo(), this.map) .generateSchema(); }
@Test public void testContainerBootstrapValidationFactory() { final Object token = new Object(); PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter(); try { Bootstrap.getEntityManagerFactoryBuilder( adapter, Collections.singletonMap(AvailableSettings.VALIDATION_FACTORY, token)); fail("Was expecting error as token did not implement ValidatorFactory"); } catch (HibernateException e) { // probably the condition we want but unfortunately the exception is not specific // and the pertinent info is in a cause } }
@Override @SuppressWarnings("unchecked") public EntityManagerFactory createContainerEntityManagerFactory( PersistenceUnitInfo info, Map properties) { final Map settings = generateSettings(properties); // OSGi ClassLoaders must implement BundleReference settings.put( org.hibernate.jpa.AvailableSettings.SCANNER, new OsgiScanner(((BundleReference) info.getClassLoader()).getBundle())); osgiClassLoader.addClassLoader(info.getClassLoader()); return Bootstrap.getEntityManagerFactoryBuilder(info, settings, osgiClassLoader).build(); }
@BeforeClassOnce @SuppressWarnings({"UnusedDeclaration"}) public void buildEntityManagerFactory() throws Exception { log.trace("Building EntityManagerFactory"); entityManagerFactoryBuilder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder( buildPersistenceUnitDescriptor(), buildSettings()); entityManagerFactory = entityManagerFactoryBuilder.build().unwrap(SessionFactoryImplementor.class); serviceRegistry = (StandardServiceRegistryImpl) entityManagerFactory.getServiceRegistry().getParentServiceRegistry(); afterEntityManagerFactoryBuilt(); }
@Test @SuppressWarnings("unchecked") public void testPersisterClassProvider() { Map settings = SettingsGenerator.generateSettings( PersisterClassResolverInitiator.IMPL_NAME, GoofyPersisterClassProvider.class, AvailableSettings.LOADED_CLASSES, Arrays.asList(Bell.class)); try { EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings) .build(); entityManagerFactory.close(); } catch (PersistenceException e) { Assert.assertNotNull(e.getCause()); Assert.assertNotNull(e.getCause().getCause()); Assert.assertEquals(GoofyException.class, e.getCause().getCause().getClass()); } }