Example usage for org.springframework.orm.jpa.persistenceunit PersistenceUnitManager obtainPersistenceUnitInfo

List of usage examples for org.springframework.orm.jpa.persistenceunit PersistenceUnitManager obtainPersistenceUnitInfo

Introduction

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

Prototype

PersistenceUnitInfo obtainPersistenceUnitInfo(String persistenceUnitName)
        throws IllegalArgumentException, IllegalStateException;

Source Link

Document

Obtain the specified PersistenceUnitInfo from this manager.

Usage

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

private Configuration determineHibernateConfiguration()
        throws MappingException, ClassNotFoundException, IOException {
    // Read in the bean definitions but don't go creating all the singletons,
    // because that causes creation of data sources and connections to database, etc.
    NonInitializingClassPathXmlApplicationContext context = new NonInitializingClassPathXmlApplicationContext(
            new String[] { emfContextLocation });
    try {/*from  ww w  .j  a v a  2s  . com*/
        context.loadBeanDefinitions();

        // Get well-known EntityManagerFactory bean definition by name
        BeanDefinition emfBeanDef = context.getBeanDefinition(emfContextBeanName);
        if (emfBeanDef == null) {
            throw new RuntimeException("no bean defined: " + emfContextBeanName);
        }

        // Get the name of the persistence unit for this EntityManagerFactory
        PropertyValue puNameProperty = emfBeanDef.getPropertyValues().getPropertyValue("persistenceUnitName");
        if (puNameProperty == null || !(puNameProperty.getValue() instanceof TypedStringValue)) {
            throw new RuntimeException(
                    "no property 'persistenceUnitName' defined on bean: " + emfContextBeanName);
        }
        String puName = ((TypedStringValue) puNameProperty.getValue()).getValue();

        // Get the name of the persistence unit for this EntityManagerFactory
        PropertyValue pumProperty = emfBeanDef.getPropertyValues().getPropertyValue("persistenceUnitManager");
        PersistenceUnitManager pum = null;
        if (pumProperty != null) {
            pum = createConfiguredPum(context, pumProperty);
        } else {
            pum = simulateDefaultPum(context, emfBeanDef);
        }

        // create the Hibernate configuration
        PersistenceUnitInfo pui = pum.obtainPersistenceUnitInfo(puName);
        Configuration configuration = new Configuration();
        configuration.setProperties(pui.getProperties());
        for (String className : pui.getManagedClassNames()) {
            configuration.addAnnotatedClass(Class.forName(className));
        }

        return configuration;
    } finally {
        context.close();
    }
}