Example usage for org.hibernate.cfg Configuration addXML

List of usage examples for org.hibernate.cfg Configuration addXML

Introduction

In this page you can find the example usage for org.hibernate.cfg Configuration addXML.

Prototype

@Deprecated
public Configuration addXML(String xml) throws MappingException 

Source Link

Usage

From source file:uk.co.modularaudio.util.hibernate.generator.GeneratorHelper.java

License:Open Source License

/**
 * Generate an appropriate DDL by loading a Spring beans.xml and searching for components implementing the IsHibernatePersistedComponent interface
 * @param beansFilename The Spring beans.xml file to read
 * @param configurationFilename The configuration filename necessary
 * @param dialectName The hibernate dialect that should be used
 * @param destinationDirectory The output directory
 * @param outputFileName The output filename
 * @throws DatastoreException A general failure during processing (such as starting up the application context)
 * @throws IOException On errors setting up the components, reading the HBM configurations or writing the output DDL file
 *///  ww  w .  j a  va 2s .c o m
public void generateFromBeansDDL(final String beansFilename, final String configurationFilename,
        final String dialectName, final String destinationDirectory, final String outputFileName)
        throws DatastoreException, IOException {
    final Configuration configuration = new Configuration();
    final SpringComponentHelper sch = new SpringComponentHelper();
    final GenericApplicationContext appContext = sch.makeAppContext(beansFilename, configurationFilename);

    final Map<String, ComponentWithHibernatePersistence> components = appContext
            .getBeansOfType(ComponentWithHibernatePersistence.class);

    for (final Iterator<ComponentWithHibernatePersistence> iter = components.values().iterator(); iter
            .hasNext();) {
        final ComponentWithHibernatePersistence component = iter.next();
        final List<HibernatePersistedBeanDefinition> hbmDefinitions = component
                .listHibernatePersistedBeanDefinitions();

        for (final HibernatePersistedBeanDefinition beanDefinition : hbmDefinitions) {
            final String originalHbmResourceName = beanDefinition.getHbmResourceName();

            final InputStream hbmInputStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(originalHbmResourceName);

            final String originalHbmString = IOUtils.basicReadInputStreamUTF8(hbmInputStream);

            final String newHbmString = TableNamePrefixer.prefixTableNames(originalHbmString,
                    beanDefinition.getPersistedBeanTablePrefix());

            configuration.addXML(newHbmString);
        }
    }
    configureAndGenerate(dialectName, destinationDirectory, outputFileName, configuration);
    appContext.close();
}