Example usage for org.springframework.orm.jpa.persistenceunit DefaultPersistenceUnitManager preparePersistenceUnitInfos

List of usage examples for org.springframework.orm.jpa.persistenceunit DefaultPersistenceUnitManager preparePersistenceUnitInfos

Introduction

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

Prototype

public void preparePersistenceUnitInfos() 

Source Link

Document

Prepare the PersistenceUnitInfos according to the configuration of this manager: scanning for persistence.xml files, parsing all matching files, configuring and post-processing them.

Usage

From source file:com.brienwheeler.apps.schematool.SchemaToolBean.java

@SuppressWarnings("unchecked")
private PersistenceUnitManager simulateDefaultPum(NonInitializingClassPathXmlApplicationContext context,
        BeanDefinition emfBeanDef) {/*from   w ww.ja  v  a  2 s.co  m*/
    // Simulate Spring's use of DefaultPersistenceUnitManager
    DefaultPersistenceUnitManager defpum = new DefaultPersistenceUnitManager();

    // Set the location of the persistence XML -- when using the DPUM,
    // you can only set one persistence XML location on the EntityManagerFactory
    PropertyValue locationProperty = emfBeanDef.getPropertyValues().getPropertyValue("persistenceXmlLocation");
    if (locationProperty == null || !(locationProperty.getValue() instanceof TypedStringValue)) {
        throw new RuntimeException(
                "no property 'persistenceXmlLocation' defined on bean: " + emfContextBeanName);
    }

    // Since PersistenceUnitPostProcessors may do things like set properties
    // onto the persistence unit, we need to instantiate them here so that
    // they get called when preparePersistenceUnitInfos() executes
    PropertyValue puiPostProcProperty = emfBeanDef.getPropertyValues()
            .getPropertyValue("persistenceUnitPostProcessors");
    if (puiPostProcProperty != null && puiPostProcProperty.getValue() instanceof ManagedList) {
        List<PersistenceUnitPostProcessor> postProcessors = new ArrayList<PersistenceUnitPostProcessor>();
        for (BeanDefinitionHolder postProcBeanDef : (ManagedList<BeanDefinitionHolder>) puiPostProcProperty
                .getValue()) {
            String beanName = postProcBeanDef.getBeanName();
            BeanDefinition beanDefinition = postProcBeanDef.getBeanDefinition();
            PersistenceUnitPostProcessor postProcessor = (PersistenceUnitPostProcessor) context
                    .createBean(beanName, beanDefinition);
            postProcessors.add(postProcessor);
        }
        defpum.setPersistenceUnitPostProcessors(
                postProcessors.toArray(new PersistenceUnitPostProcessor[postProcessors.size()]));
    }

    defpum.setPersistenceXmlLocation(((TypedStringValue) locationProperty.getValue()).getValue());
    defpum.preparePersistenceUnitInfos();

    return defpum;
}