Example usage for javax.persistence.spi PersistenceUnitInfo getManagedClassNames

List of usage examples for javax.persistence.spi PersistenceUnitInfo getManagedClassNames

Introduction

In this page you can find the example usage for javax.persistence.spi PersistenceUnitInfo getManagedClassNames.

Prototype

public List<String> getManagedClassNames();

Source Link

Document

Returns the list of the names of the classes that the persistence provider must add to its set of managed classes.

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 {/*  w ww  .ja  v a  2 s. co  m*/
        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();
    }
}