Example usage for org.springframework.orm.jpa.persistenceunit PersistenceUnitPostProcessor PersistenceUnitPostProcessor

List of usage examples for org.springframework.orm.jpa.persistenceunit PersistenceUnitPostProcessor PersistenceUnitPostProcessor

Introduction

In this page you can find the example usage for org.springframework.orm.jpa.persistenceunit PersistenceUnitPostProcessor PersistenceUnitPostProcessor.

Prototype

PersistenceUnitPostProcessor

Source Link

Usage

From source file:org.ngrinder.infra.config.DatabaseConfig.java

/**
 * Create {@link LocalContainerEntityManagerFactoryBean} bean for Hibernate. Hibernate doesn't
 * support the search for the {@link Entity} classes in the other Jar files. This method
 * directly searches the {@link Entity} classes with {@link Reflections} not using Hibernate
 * entity class search feature to overcome the limitation
 * <p/>/*from w ww.ja v  a 2  s. c  o m*/
 * use annotation DependsOn to insure after databaseUpdater is
 *
 * @return {@link LocalContainerEntityManagerFactoryBean}
 */
@Bean(name = "emf")
@DependsOn("databaseUpdater")
public LocalContainerEntityManagerFactoryBean emf() {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource());
    emf.setPersistenceUnitName("ngrinder");
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    PropertiesWrapper databaseProperties = config.getDatabaseProperties();

    Database database = Database.getDatabase(databaseProperties.getProperty(PROP_DATABASE_TYPE));
    if (config.isClustered() && !database.isClusterSupport()) {
        CoreLogger.LOGGER.error("In cluster mode, H2 is not allowed to use. Please select cubrid as database");
    }
    hibernateJpaVendorAdapter.setDatabasePlatform(database.getDialect());
    hibernateJpaVendorAdapter.setShowSql(false);
    emf.setJpaVendorAdapter(hibernateJpaVendorAdapter);
    // To search entity packages from other jar files..
    emf.setPackagesToScan("empty");
    emf.setPersistenceUnitPostProcessors(new PersistenceUnitPostProcessor() {
        @Override
        public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
            Reflections reflections = new Reflections(ControllerConstants.DEFAULT_PACKAGE_NAME);
            for (Class<?> each : reflections.getTypesAnnotatedWith(Entity.class)) {
                LOGGER.trace("Entity class {} is detected as the SpringData entity.", each.getName());
                pui.addManagedClassName(each.getName());
            }
        }
    });
    return emf;
}