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.easybatch.extensions.hibernate.DatabaseUtil.java

License:Open Source License

public static void initializeSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure("/org/easybatch/extensions/hibernate/hibernate.cfg.xml");

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.easybatch.integration.hibernate.DatabaseUtil.java

License:Open Source License

public static void initializeSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure("/org/easybatch/integration/hibernate/hibernate.cfg.xml");

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.fhcrc.cpl.viewer.database.HibernateManager.java

License:Apache License

public HibernateManager() {
    try {//from  w w w . j av a2 s .  c om
        // Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration();
        if (configFile == null)
            retrieveOrPromptConfigFilePreference();
        if (configFile != null)
            configuration.configure(configFile);
        sessionFactory = configuration.buildSessionFactory();
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:org.freaksparty.ficonbar.util.DBSession.java

public static Session getSession() {
    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties());
    SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build());
    Session session = sessionFactory.openSession();
    return session;
}

From source file:org.grouter.common.hibernate.HibernateUtilContextAware.java

License:Apache License

/**
 * Create session factories and configurations and store in hibernateConfigMap. On
 * completion we enter INITIALISED state.
 */// ww w .  j a  v a  2s.c  o  m
private static void createSessionFactoriesFromConfigMap() {
    // read in all config and create session factories
    Iterator iter = hibernateConfigMap.keySet().iterator();
    while (iter.hasNext()) {
        SessionFactory sessionFactory;
        String key = (String) iter.next();
        HibernateConfigItem hibernateConfigItem = hibernateConfigMap.get(key);
        String file = hibernateConfigItem.getConfigFile();
        Configuration configuration;
        if (file == null) {
            log.info("Loading properties config and not from file ");
            configuration = hibernateConfigItem.getConfiguration();
        } else {
            log.info("Loading properties from : " + file);
            configuration = new Configuration();
            configuration = configuration.configure(file);
        }
        try {
            String sessionFactoryName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
            if (sessionFactoryName != null) {
                log.debug("Looking up SessionFactory in JNDI with name : " + sessionFactoryName);
                try {
                    Hashtable env = new Hashtable();
                    env.put(Context.INITIAL_CONTEXT_FACTORY, configuration.getProperty(Environment.JNDI_CLASS));
                    env.put(Context.URL_PKG_PREFIXES, configuration.getProperty(Environment.JNDI_PREFIX));
                    env.put(Context.PROVIDER_URL, configuration.getProperty(Environment.JNDI_URL));
                    Context context = new InitialContext(env);
                    JNDIUtils.printJNDI(context, log);
                    sessionFactory = (SessionFactory) context.lookup(sessionFactoryName);
                    if (sessionFactory == null) {
                        throw new IllegalStateException(
                                "SessionFactory from JNDI lookup returned a null implemenation  using file : "
                                        + file);
                    }
                } catch (NamingException ex) {
                    log.error("Failed looking up sessinfactory : " + sessionFactoryName, ex);
                    throw new RuntimeException(ex);
                }
            } else {
                sessionFactory = configuration.buildSessionFactory();
                if (sessionFactory == null) {
                    throw new IllegalStateException(
                            "SessionFactory could not be createed from the configuration using file : " + file);
                }
            }
            hibernateConfigItem.setConfiguration(configuration);
            hibernateConfigItem.setSessionFactory(sessionFactory);
            // We need to have a default sessionfactory / configuration
            if (hibernateConfigItem.isDeafult()) {
                hibernateConfigItemDefault = hibernateConfigItem;
            }
            // setInterceptor(configuration, null);
            // hibernateConfigMap.put(key)
        } catch (Throwable ex) {
            log.error("Failed initializing from configuration.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    currentState = STATE.INITIALISED;
    log.info("Entered state : " + currentState);
}

From source file:org.jboss.additional.testsuite.jdkall.past.jpa.hibernate.secondlevelcache.SFSB.java

License:Open Source License

@TransactionAttribute(TransactionAttributeType.NEVER)
public void setupConfig() {
    // static {/*from  w ww  . j  a  v  a 2s. com*/
    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.getProperties().put(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta");
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        // 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
        ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:org.jboss.as.test.compat.jpa.hibernate.transformer.SFSBHibernateSessionFactory.java

License:Open Source License

public void setupConfig() {
    // static {/*from w ww. j a v  a 2 s.  c o m*/
    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
        throw new RuntimeException("Could not setup config", ex);
    }

}

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

License:Open Source License

public void setupConfig() {
    // static {/*from  w  w  w  .j  av  a  2s  .c o  m*/
    try {

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

        // 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");
        // 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);
        throw new ExceptionInInitializerError(ex);
    }

}

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

License:Open Source License

public void setupConfig() {
    // static {//from w  w  w . jav a 2s.  co m
    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.configure("hibernate.cfg.xml");

        // fetch the properties
        Properties properties = new Properties();
        properties.putAll(configuration.getProperties());

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

        // build the serviceregistry
        final BootstrapServiceRegistryBuilder bootstrapbuilder = new BootstrapServiceRegistryBuilder();
        serviceRegistry = builder.build();

        // Create the SessionFactory from Configuration
        sessionFactory = configuration.configure("hibernate.cfg.xml").buildSessionFactory(serviceRegistry);

    } 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.secondlevelcache.SFSB.java

License:Open Source License

@TransactionAttribute(TransactionAttributeType.NEVER)
public void setupConfig() {
    // static {/*from ww  w  . j  a  v a 2s  .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");
        // 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);
        throw new ExceptionInInitializerError(ex);
    }
}