@Bean @ConditionalOnMissingBean public EntityManagerFactoryBuilder entityManagerFactoryBuilder( JpaVendorAdapter jpaVendorAdapter, ObjectProvider<PersistenceUnitManager> persistenceUnitManagerProvider) { EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder( jpaVendorAdapter, this.properties.getProperties(), persistenceUnitManagerProvider.getIfAvailable()); builder.setCallback(getVendorCallback()); return builder; }
@Test public void customPersistenceUnitManager() throws Exception { setupTestConfiguration(TestConfigurationWithCustomPersistenceUnitManager.class); this.context.refresh(); LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = this.context .getBean(LocalContainerEntityManagerFactoryBean.class); Field field = LocalContainerEntityManagerFactoryBean.class .getDeclaredField("persistenceUnitManager"); field.setAccessible(true); assertThat(field.get(entityManagerFactoryBean)) .isEqualTo(this.context.getBean(PersistenceUnitManager.class)); }
/** * Package private so that only PersistenceProperties class can initialize this one. */ public PersistenceUnitContext(final PersistenceUnitManager persistenceUnitManager, final PersistenceUnitContextManager persistenceUnitContextManager, final String persistenceUnitName) { this.persistenceUnitManager = persistenceUnitManager; this.persistenceUnitContextManager = persistenceUnitContextManager; this.systemProperties = new SystemProperties(getClass()); this.persistenceUnitName = persistenceUnitName; }
@Bean(name="corePersistenceUnitManager") @Primary public PersistenceUnitManager corePersistenceUnitManager(DatabaseSelector dataSource) { DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); persistenceUnitManager.setDefaultDataSource(dataSource); persistenceUnitManager.setPackagesToScan("edu.cmu.cs.lti.discoursedb.core.model"); persistenceUnitManager.setDefaultPersistenceUnitName("corePersistenceUnitManager"); return persistenceUnitManager; }
@Bean(name="systemPersistenceUnitManager") public PersistenceUnitManager systemPersistenceUnitManager() { DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); persistenceUnitManager.setDefaultDataSource(this.systemDataSource()); persistenceUnitManager.setPackagesToScan("edu.cmu.cs.lti.discoursedb.system.model"); persistenceUnitManager.setDefaultPersistenceUnitName("systemPersistenceUnitManager"); return persistenceUnitManager; }
@Test public void customPersistenceUnitManager() throws Exception { setupTestConfiguration(TestConfigurationWithCustomPersistenceUnitManager.class); this.context.refresh(); LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = this.context .getBean(LocalContainerEntityManagerFactoryBean.class); Field field = LocalContainerEntityManagerFactoryBean.class .getDeclaredField("persistenceUnitManager"); field.setAccessible(true); assertThat(field.get(entityManagerFactoryBean), equalTo((Object) this.context.getBean(PersistenceUnitManager.class))); }
/** * @return Introduce custom persistence unit manager so that we can use the orm.xml file to rename the table * mapping for Student.java and verify that we pick up the renamed mapping via ProviderAccess. */ @Bean public PersistenceUnitManager persistenceUnitManager(DataSource dataSource) { DefaultPersistenceUnitManager manager = new DefaultPersistenceUnitManager(); manager.setPersistenceXmlLocation("classpath:META-INF/pedal-test-persistence.xml"); manager.setDefaultDataSource(dataSource); return manager; }
private PersistenceUnitManager preparePersistentUnitManager(Config config, String prefix, DataSource datasource) { DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); persistenceUnitManager.setDefaultDataSource(datasource); persistenceUnitManager.setPersistenceXmlLocations(new String[] {determineConfigProperty(config, prefix, "PersistenceXmlLocation", "META-INF/persistence.xml")}); persistenceUnitManager.setDefaultPersistenceUnitRootLocation(determineConfigProperty(config, prefix, "PersistenceUnitRootLocation", "classpath:")); RicePersistenceUnitPostProcessor postProcessor = new RicePersistenceUnitPostProcessor(); postProcessor.setJtaDataSource(datasource); persistenceUnitManager.setPersistenceUnitPostProcessors(new RicePersistenceUnitPostProcessor[] { postProcessor }); persistenceUnitManager.afterPropertiesSet(); return persistenceUnitManager; }
public PersistenceUnitContextManager(final PersistenceUnitManager persistenceUnitManager, final IDialectSpecificDelegate[] dialectSpecificDelegates) { Assertions.assertThat(persistenceUnitManager).isNotNull(); this.persistenceUnitManager = persistenceUnitManager; associateDelegates(dialectSpecificDelegates); }
Delegate(JpaVendorAdapter jpaVendorAdapter, Map<String, ?> jpaProperties, PersistenceUnitManager persistenceUnitManager) { super(jpaVendorAdapter, jpaProperties, persistenceUnitManager); }
/** * Determine the PersistenceUnitInfo to use for the EntityManagerFactory * created by this bean. * <p>The default implementation reads in all persistence unit infos from * {@code persistence.xml}, as defined in the JPA specification. * If no entity manager name was specified, it takes the first info in the * array as returned by the reader. Otherwise, it checks for a matching name. * @param persistenceUnitManager the PersistenceUnitManager to obtain from * @return the chosen PersistenceUnitInfo */ protected PersistenceUnitInfo determinePersistenceUnitInfo(PersistenceUnitManager persistenceUnitManager) { if (getPersistenceUnitName() != null) { return persistenceUnitManager.obtainPersistenceUnitInfo(getPersistenceUnitName()); } else { return persistenceUnitManager.obtainDefaultPersistenceUnitInfo(); } }
/** * Create a new instance passing in the common pieces that will be shared if multiple * EntityManagerFactory instances are created. * @param jpaVendorAdapter a vendor adapter * @param jpaProperties JPA properties to be passed to the persistence provider. * @param persistenceUnitManager optional source of persistence unit information (can * be null) */ public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter, Map<String, ?> jpaProperties, PersistenceUnitManager persistenceUnitManager) { this.jpaVendorAdapter = jpaVendorAdapter; this.persistenceUnitManager = persistenceUnitManager; this.jpaProperties = new LinkedHashMap<String, Object>(jpaProperties); }
/** * Set the PersistenceUnitManager to use for obtaining the JPA persistence unit * that this FactoryBean is supposed to build an EntityManagerFactory for. * <p>The default is to rely on the local settings specified on this FactoryBean, * such as "persistenceXmlLocation", "dataSource" and "loadTimeWeaver". * <p>For reuse of existing persistence unit configuration or more advanced forms * of custom persistence unit handling, consider defining a separate * PersistenceUnitManager bean (typically a DefaultPersistenceUnitManager instance) * and linking it in here. {@code persistence.xml} location, DataSource * configuration and LoadTimeWeaver will be defined on that separate * DefaultPersistenceUnitManager bean in such a scenario. * @see #setPersistenceXmlLocation * @see #setDataSource * @see #setLoadTimeWeaver * @see org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager */ public void setPersistenceUnitManager(PersistenceUnitManager persistenceUnitManager) { this.persistenceUnitManager = persistenceUnitManager; }
/** * Creates a JPA-specific entity manager factory bean. * * @param manager the persistence unit manager to use. * @return a JPA-specific entity manager factory bean. */ protected LocalContainerEntityManagerFactoryBean createInternalFactoryBean(PersistenceUnitManager manager) { LocalContainerEntityManagerFactoryBean delegate = new LocalContainerEntityManagerFactoryBean(); delegate.setPersistenceUnitManager(manager); return delegate; }
/** * Create a new instance passing in the common pieces that will be shared if multiple * EntityManagerFactory instances are created. * @param jpaVendorAdapter a vendor adapter * @param properties common configuration options, including generic map for JPA * vendor properties * @param persistenceUnitManager optional source of persistence unit information (can * be null) */ public EntityManagerFactoryBuilder(JpaVendorAdapter jpaVendorAdapter, JpaProperties properties, PersistenceUnitManager persistenceUnitManager) { this.delegate = new Delegate(jpaVendorAdapter, properties.getProperties(), persistenceUnitManager); }