下面列出了怎么用org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Creates a default KRAD-managed factory bean.
*/
public KradEntityManagerFactoryBean() {
this.persistenceUnitPostProcessors = new ArrayList<PersistenceUnitPostProcessor>();
this.managedClassNames = new ArrayList<String>();
this.converterPackageNames = new ArrayList<String>();
converterPackageNames.add(DEFAULT_CONVERTERS_PACKAGE); // set as default
this.persistenceUnitManager = createPersistenceUnitManager();
this.internalFactoryBean = createInternalFactoryBean(this.persistenceUnitManager);
this.internalFactoryBean.setJpaPropertyMap(createDefaultJpaProperties());
}
public PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors() {
return postProcessors;
}
public void setPersistenceUnitPostProcessors(final PersistenceUnitPostProcessor... postProcessors) {
this.postProcessors = postProcessors;
}
/**
* Initialize JPA objects (Datasource, Persistence Unit Manager, Entity Manager Factory, Transaction Manager) for each pool.
*/
public void process( )
{
ReferenceList list = new ReferenceList( );
AppConnectionService.getPoolList( list );
Map<String, EntityManagerFactory> mapFactories = new HashMap<>( );
List<PlatformTransactionManager> listTransactionManagers = new ArrayList<>( );
_log.info( "JPA Startup Service : Initializing JPA objects ..." );
String strDialectProperty = AppPropertiesService.getProperty( JPA_DIALECT_PROPERTY );
for ( ReferenceItem poolItem : list )
{
String strPoolname = poolItem.getCode( );
DataSource ds = AppConnectionService.getPoolManager( ).getDataSource( strPoolname );
_log.info( "JPA Startup Service : DataSource retrieved for pool : " + strPoolname );
_log.debug( "> DS : " + ds.toString( ) );
DefaultPersistenceUnitManager pum = new DefaultPersistenceUnitManager( );
pum.setDefaultDataSource( ds );
PersistenceUnitPostProcessor [ ] postProcessors = {
new JPAPersistenceUnitPostProcessor( )
};
pum.setPersistenceUnitPostProcessors( postProcessors );
pum.afterPropertiesSet( );
_log.info( "JPA Startup Service : Persistence Unit Manager for pool : " + strPoolname );
_log.debug( "> PUM : " + pum.toString( ) );
LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean( );
lcemfb.setDataSource( ds );
lcemfb.setPersistenceUnitManager( pum );
lcemfb.setPersistenceUnitName( "jpaLuteceUnit" );
JpaDialect jpaDialect = SpringContextService.getBean( "jpaDialect" );
lcemfb.setJpaDialect( jpaDialect );
Map mapJpaProperties = SpringContextService.getBean( "jpaPropertiesMap" );
lcemfb.setJpaPropertyMap( mapJpaProperties );
String strDialect = AppPropertiesService.getProperty( poolItem.getName( ) + ".dialect" );
// replace default dialect if <poolname>.dialect is specified
if ( StringUtils.isNotBlank( strDialect ) )
{
mapJpaProperties.put( strDialectProperty, strDialect );
}
_log.debug( "Using dialect " + mapJpaProperties.get( strDialectProperty ) + " for pool " + poolItem.getName( ) );
JpaVendorAdapter jpaVendorAdapter = SpringContextService.getBean( "jpaVendorAdapter" );
lcemfb.setJpaVendorAdapter( jpaVendorAdapter );
lcemfb.afterPropertiesSet( );
EntityManagerFactory emf = lcemfb.getNativeEntityManagerFactory( );
_log.info( "JPA Startup Service : EntityManagerFactory created for pool : " + strPoolname );
_log.debug( "> EMF : " + emf.toString( ) );
JpaTransactionManager tm = new JpaTransactionManager( );
tm.setEntityManagerFactory( emf );
tm.setJpaDialect( jpaDialect );
_log.debug( "> JpaDialect " + jpaDialect );
tm.afterPropertiesSet( );
_log.info( "JPA Startup Service : JPA TransactionManager created for pool : " + strPoolname );
_log.debug( "> TM : " + tm.toString( ) );
mapFactories.put( strPoolname, emf );
listTransactionManagers.add( tm );
}
EntityManagerService ems = SpringContextService.getBean( "entityManagerService" );
ems.setMapFactories( mapFactories );
ChainedTransactionManager ctm = SpringContextService.getBean( "transactionManager" );
ctm.setTransactionManagers( listTransactionManagers );
_log.info( "JPA Startup Service : completed successfully" );
}
/**
* Assembles the {@link PersistenceUnitPostProcessor}s into an array.
*
* @return an array of the {@link PersistenceUnitPostProcessor}s.
*/
protected PersistenceUnitPostProcessor[] assemblePersistenceUnitPostProcessors() {
this.persistenceUnitPostProcessors = new ArrayList<PersistenceUnitPostProcessor>(this.persistenceUnitPostProcessors);
this.persistenceUnitPostProcessors.add(new InternalPersistenceUnitPostProcessor());
return this.persistenceUnitPostProcessors.toArray(new PersistenceUnitPostProcessor[this.persistenceUnitPostProcessors.size()]);
}
/**
* Set the PersistenceUnitPostProcessors to be applied to the
* PersistenceUnitInfo used for creating this EntityManagerFactory.
* <p>Such post-processors can, for example, register further entity
* classes and jar files, in addition to the metadata read from
* {@code persistence.xml}.
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see #setPersistenceUnitManager
*/
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
this.internalPersistenceUnitManager.setPersistenceUnitPostProcessors(postProcessors);
}
/**
* Set the PersistenceUnitPostProcessors to be applied to the
* PersistenceUnitInfo used for creating this EntityManagerFactory.
* <p>Such post-processors can, for example, register further entity
* classes and jar files, in addition to the metadata read from
* {@code persistence.xml}.
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see #setPersistenceUnitManager
*/
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
this.internalPersistenceUnitManager.setPersistenceUnitPostProcessors(postProcessors);
}
/**
* Set the PersistenceUnitPostProcessors to be applied to the
* PersistenceUnitInfo used for creating this EntityManagerFactory.
* <p>Such post-processors can, for example, register further entity
* classes and jar files, in addition to the metadata read from
* {@code persistence.xml}.
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see #setPersistenceUnitManager
*/
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
this.internalPersistenceUnitManager.setPersistenceUnitPostProcessors(postProcessors);
}
/**
* Set the PersistenceUnitPostProcessors to be applied to the
* PersistenceUnitInfo used for creating this EntityManagerFactory.
* <p>Such post-processors can, for example, register further entity
* classes and jar files, in addition to the metadata read from
* {@code persistence.xml}.
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see #setPersistenceUnitManager
*/
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
this.internalPersistenceUnitManager.setPersistenceUnitPostProcessors(postProcessors);
}
/**
* Returns an array of the {@link PersistenceUnitPostProcessor} instances which have been configured on this
* factory bean.
*
* @return array of post processors, may be empty but will never return null
*/
public PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors() {
return persistenceUnitPostProcessors.toArray(new PersistenceUnitPostProcessor[persistenceUnitPostProcessors.size()]);
}
/**
* Set the PersistenceUnitPostProcessors to be applied to the * PersistenceUnitInfo used for creating this
* EntityManagerFactory.
*
* <p>
* Note that if executed before {@link #afterPropertiesSet()} then this factory bean may introduce its own post
* processor instances. If invoked after, then this method will override internally configured post processors.
* </p>
*
* <p>
* Such post-processors can, for example, register further entity classes and jar files, in addition to the metadata
* read from {@code persistence.xml}.
* </p>
*
* @param postProcessors one or more post processors to set
*/
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
// persistence unit post processors will get applied to the internal factory bean during afterPropertiesSet(),
// this allows us to add our own internal post processor to the list)
this.persistenceUnitPostProcessors = new ArrayList<PersistenceUnitPostProcessor>(Arrays.asList(postProcessors));
}