Example usage for org.hibernate.cfg Configuration configure

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

Introduction

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

Prototype

@Deprecated
public Configuration configure(org.w3c.dom.Document document) throws HibernateException 

Source Link

Usage

From source file:org.jboss.as.test.integration.hibernate.SFSBHibernate2LcacheStats.java

License:Open Source License

@TransactionAttribute(TransactionAttributeType.NEVER)
public void setupConfig() {
    // static {// w w  w.j a v a  2  s .  c  om
    try {

        //System.out.println("setupConfig:  Current dir = " + (new File(".")).getCanonicalPath());

        // prepare the configuration
        Configuration configuration = new Configuration()
                .setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
        configuration.getProperties().put(AvailableSettings.JTA_PLATFORM, JBossAppServerJtaPlatform.class);
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        // set property to enable statistics
        configuration.setProperty("hibernate.generate_statistics", "true");

        // fetch the properties
        Properties properties = new Properties();
        configuration = configuration.configure("hibernate.cfg.xml");
        properties.putAll(configuration.getProperties());

        Environment.verifyProperties(properties);
        ConfigurationHelper.resolvePlaceHolders(properties);

        // build the serviceregistry
        sessionFactory = configuration.buildSessionFactory();
    } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }

    //System.out.println("setupConfig: done");

}

From source file:org.jboss.as.test.integration.hibernate.SFSBHibernateSessionFactory.java

License:Open Source License

public void setupConfig() {
    // static {/*from w  w  w.ja v  a 2  s . c  om*/
    try {

        // prepare the configuration
        Configuration configuration = new Configuration()
                .setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        configuration.setProperty("hibernate.listeners.envers.autoRegister", "false");

        // fetch the properties
        Properties properties = new Properties();
        configuration = configuration.configure("hibernate.cfg.xml");
        properties.putAll(configuration.getProperties());

        Environment.verifyProperties(properties);
        ConfigurationHelper.resolvePlaceHolders(properties);

        sessionFactory = configuration.buildSessionFactory();
    } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        // ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }

}

From source file:org.jboss.as.test.integration.hibernate.SFSBHibernateTransaction.java

License:Open Source License

public void setupConfig() {
    // static {/*from w w w  . j  a  va2  s .  c  om*/
    try {

        // prepare the configuration
        Configuration configuration = new Configuration()
                .setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
        configuration.getProperties().put(AvailableSettings.JTA_PLATFORM, JBossAppServerJtaPlatform.class);
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        configuration.setProperty("hibernate.listeners.envers.autoRegister", "false");

        // fetch the properties
        Properties properties = new Properties();
        configuration = configuration.configure("hibernate.cfg.xml");
        properties.putAll(configuration.getProperties());
        Environment.verifyProperties(properties);
        ConfigurationHelper.resolvePlaceHolders(properties);

        sessionFactory = configuration.buildSessionFactory();
    } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        // ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }

}

From source file:org.jboss.as.test.integration.hibernate.SFSBHibernatewithCriteriaSession.java

License:Open Source License

public void setupConfig() {
    // static {/*from ww w .  j  a  v  a2s.c o m*/
    try {

        // prepare the configuration
        Configuration configuration = new Configuration()
                .setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
        configuration.getProperties().put(AvailableSettings.JTA_PLATFORM, JBossAppServerJtaPlatform.class);
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        configuration.setProperty("hibernate.listeners.envers.autoRegister", "false");

        // fetch the properties
        Properties properties = new Properties();
        configuration = configuration.configure("hibernate.cfg.xml");
        properties.putAll(configuration.getProperties());

        Environment.verifyProperties(properties);
        ConfigurationHelper.resolvePlaceHolders(properties);

        // build the serviceregistry
        StandardServiceRegistryBuilder registry = new StandardServiceRegistryBuilder()
                .applySettings(properties);
        sessionFactory = configuration.buildSessionFactory(registry.build());

        // build metamodel
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
        MetamodelImpl.buildMetamodel(configuration.getClassMappings(), sfi);

        sessionFactory.getStatistics().setStatisticsEnabled(true);

    } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        // ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }

}

From source file:org.jboss.as.test.integration.hibernate.SFSBHibernatewithMetaDataSession.java

License:Open Source License

public void setupConfig() {
    // static {/*w  ww. jav  a  2s .  c om*/
    try {

        // prepare the configuration
        Configuration configuration = new Configuration()
                .setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        configuration.setProperty("hibernate.listeners.envers.autoRegister", "false");

        // fetch the properties
        Properties properties = new Properties();
        configuration = configuration.configure("hibernate.cfg.xml");
        properties.putAll(configuration.getProperties());

        Environment.verifyProperties(properties);
        ConfigurationHelper.resolvePlaceHolders(properties);

        StandardServiceRegistryBuilder registry = new StandardServiceRegistryBuilder()
                .applySettings(properties);
        sessionFactory = configuration.buildSessionFactory(registry.build());

        // build metamodel
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
        MetamodelImpl.buildMetamodel(configuration.getClassMappings(), sfi);

        sessionFactory.getStatistics().setStatisticsEnabled(true);

    } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        // ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }

}

From source file:org.jboss.tools.hibernate3_6.ConfigurationFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
private Configuration loadConfigurationXML(Configuration localCfg, boolean includeMappings,
        EntityResolver entityResolver) {
    File configXMLFile = prefs.getConfigXMLFile();
    if (!includeMappings) {
        org.dom4j.Document doc;//from w  w  w  .j  ava  2s .  c  om
        XMLHelper xmlHelper = new XMLHelper();
        InputStream stream = null;
        String resourceName = "<unknown>"; //$NON-NLS-1$
        if (configXMLFile != null) {
            resourceName = configXMLFile.toString();
            try {
                stream = new FileInputStream(configXMLFile);
            } catch (FileNotFoundException e1) {
                throw new HibernateConsoleRuntimeException(
                        ConsoleMessages.ConsoleConfiguration_could_not_access + configXMLFile, e1);
            }
        } else {
            resourceName = "/hibernate.cfg.xml"; //$NON-NLS-1$
            if (checkHibernateResoureExistence(resourceName)) {
                stream = ConfigHelper.getResourceAsStream(resourceName); // simulate hibernate's
                // default look up
            } else {
                return localCfg;
            }
        }
        try {
            List<Throwable> errors = new ArrayList<Throwable>();
            doc = xmlHelper.createSAXReader(resourceName, errors, entityResolver).read(new InputSource(stream));
            if (errors.size() != 0) {
                throw new MappingException(ConsoleMessages.ConsoleConfiguration_invalid_configuration,
                        errors.get(0));
            }
            List<Node> list = doc.getRootElement().element("session-factory").elements("mapping"); //$NON-NLS-1$ //$NON-NLS-2$
            for (Node element : list) {
                element.getParent().remove(element);
            }
            DOMWriter dw = new DOMWriter();
            Document document = dw.write(doc);
            return localCfg.configure(document);

        } catch (DocumentException e) {
            throw new HibernateException(
                    ConsoleMessages.ConsoleConfiguration_could_not_parse_configuration + resourceName, e);
        } finally {
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException ioe) {
                // log.warn( "could not close input stream for: " + resourceName, ioe );
            }
        }
    } else {
        if (configXMLFile != null) {
            return localCfg.configure(configXMLFile);
        } else {
            Configuration resultCfg = localCfg;
            if (checkHibernateResoureExistence("/hibernate.cfg.xml")) { //$NON-NLS-1$
                resultCfg = localCfg.configure();
            }
            return resultCfg;
        }
    }
}

From source file:org.jbpm.ant.AntHelper.java

License:Open Source License

public static Configuration getConfiguration(String hibernateCfgResource, String hibernatePropertiesResource) {
    Object key = getKey(hibernateCfgResource, hibernatePropertiesResource);
    Configuration configuration = (Configuration) configurations.get(key);
    if (configuration == null) {
        log.debug("creating hibernate configuration from cfg '" + hibernateCfgResource + "' and properties '"
                + hibernatePropertiesResource + "'");
        configuration = new Configuration();
        configuration.configure(hibernateCfgResource);
        if (hibernatePropertiesResource != null) {
            try {
                InputStream propertiesInputStream = AntHelper.class.getClassLoader()
                        .getResourceAsStream(hibernatePropertiesResource);
                log.debug("properties input stream: " + propertiesInputStream);
                Properties properties = new Properties();
                properties.load(propertiesInputStream);
                configuration.setProperties(properties);
            } catch (Exception e) {
                throw new JbpmException("couldn't set properties '" + hibernatePropertiesResource + "'", e);
            }/*  w ww.j a  v a2 s  .  co m*/
        }
        configurations.put(key, configuration);
    } else {
        log.debug("got hibernate configuration from cfg '" + hibernateCfgResource + "' and properties '"
                + hibernatePropertiesResource + "' from the cache");
    }
    return configuration;
}

From source file:org.jbpm.db.hibernate.HibernateHelper.java

License:Open Source License

public static Configuration createConfiguration(String cfgXmlResource, String propertiesResource) {
    Configuration configuration = new Configuration();

    // if a special hibernate configuration xml file is specified, 
    if (cfgXmlResource != null) {
        // use the configured file name
        log.debug("creating hibernate configuration resource '" + cfgXmlResource + "'");
        configuration.configure(cfgXmlResource);
    } else {/*from  ww  w.  j  a va  2  s .c  o m*/
        log.debug("using default hibernate configuration resource (hibernate.cfg.xml)");
        configuration.configure();
    }

    // if the properties are specified in a separate file
    if (propertiesResource != null) {
        log.debug("using hibernate properties from resource '" + propertiesResource + "'");
        // load the properties
        Properties properties = loadPropertiesFromResource(propertiesResource);
        // and overwrite the properties with the specified properties
        configuration.setProperties(properties);
    }

    return configuration;
}

From source file:org.jbpm.db.JbpmSchema.java

License:Open Source License

static Configuration createConfiguration(String[] args, int index) {
    String hibernateCfgXml = (args.length > index ? args[index] : "hibernate.cfg.xml");
    String hibernateProperties = (args.length > (index + 1) ? args[index + 1] : null);

    Configuration configuration = new Configuration();
    configuration.configure(new File(hibernateCfgXml));
    if (hibernateProperties != null) {
        try {/* www  . j ava2s .  c om*/
            Properties properties = new Properties();
            InputStream inputStream = new FileInputStream(hibernateProperties);
            properties.load(inputStream);
            configuration.setProperties(properties);
        } catch (IOException e) {
            throw new JbpmException("couldn't load hibernate configuration", e);
        }
    }

    return configuration;
}

From source file:org.jbpm.db.JbpmSessionFactory.java

License:Open Source License

public static Configuration createConfiguration(String configResource) {
    Configuration configuration = null;
    // create the hibernate configuration
    configuration = new Configuration();
    if (configResource != null) {
        log.debug("using '" + configResource + "' as hibernate configuration for jbpm");
        configuration.configure(configResource);
    } else {/*from w  w w .  ja  v a  2 s.c o  m*/
        log.debug("using the default hibernate configuration file: hibernate.cfg.xml");
        configuration.configure();
    }

    // check if the properties in the hibernate.cfg.xml need to be overwritten by a separate properties file.
    if (JbpmConfiguration.Configs.hasObject("resource.hibernate.properties")) {
        String hibernatePropertiesResource = JbpmConfiguration.Configs
                .getString("resource.hibernate.properties");
        Properties hibernateProperties = new Properties();
        try {
            hibernateProperties.load(ClassLoaderUtil.getStream(hibernatePropertiesResource));
        } catch (IOException e) {
            throw new JbpmException("couldn't load the hibernate properties from resource '"
                    + hibernatePropertiesResource + "'", e);
        }
        log.debug("overriding hibernate properties with " + hibernateProperties);
        configuration.setProperties(hibernateProperties);
    }

    return configuration;
}