Java Code Examples for org.hibernate.cfg.Configuration#addProperties()

The following examples show how to use org.hibernate.cfg.Configuration#addProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchedulerModule.java    From flux with Apache License 2.0 6 votes vote down vote up
/**
 * Creates hibernate configuration from the configuration yaml properties.
 * Since the yaml properties are already flattened in input param <code>yamlConfiguration</code>
 * the method loops over them to selectively pick Hibernate specific properties.
 */
@Provides
@Singleton
@Named("schedulerHibernateConfiguration")
public Configuration getConfiguration(YamlConfiguration yamlConfiguration) {
    Configuration configuration = new Configuration();
    addAnnotatedClassesAndTypes(configuration);
    org.apache.commons.configuration.Configuration hibernateConfig = yamlConfiguration.subset(FLUX_SCHEDULER_HIBERNATE_CONFIG_NAME_SPACE);
    Iterator<String> propertyKeys = hibernateConfig.getKeys();
    Properties configProperties = new Properties();
    while (propertyKeys.hasNext()) {
        String propertyKey = propertyKeys.next();
        Object propertyValue = hibernateConfig.getProperty(propertyKey);
        configProperties.put(propertyKey, propertyValue);
    }
    configuration.addProperties(configProperties);
    return configuration;
}
 
Example 2
Source File: ShardModule.java    From flux with Apache License 2.0 5 votes vote down vote up
private Configuration getConfiguration(YamlConfiguration yamlConfiguration, String prefix, String host) {
    Configuration configuration = new Configuration();
    addAnnotatedClassesAndTypes(configuration);
    org.apache.commons.configuration.Configuration hibernateConfig = yamlConfiguration.subset(prefix);
    Iterator<String> propertyKeys = hibernateConfig.getKeys();
    Properties configProperties = new Properties();
    while (propertyKeys.hasNext()) {
        String propertyKey = propertyKeys.next();
        Object propertyValue = hibernateConfig.getProperty(propertyKey);
        configProperties.put(propertyKey, propertyValue);
    }
    configProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + "/flux");
    configuration.addProperties(configProperties);
    return configuration;
}
 
Example 3
Source File: RMDBManager.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
private static RMDBManager createUsingProperties() {
    if (System.getProperty(RM_DATABASE_IN_MEMORY) != null) {
        return createInMemoryRMDBManager();
    } else {
        File configFile = new File(PAResourceManagerProperties.getAbsolutePath(PAResourceManagerProperties.RM_DB_HIBERNATE_CONFIG.getValueAsString()));

        boolean drop = PAResourceManagerProperties.RM_DB_HIBERNATE_DROPDB.getValueAsBoolean();
        boolean dropNS = PAResourceManagerProperties.RM_DB_HIBERNATE_DROPDB_NODESOURCES.getValueAsBoolean();

        if (logger.isInfoEnabled()) {
            logger.info("Starting RM DB Manager " + "with drop DB = " + drop + " and drop nodesources = " + dropNS +
                        " and configuration file = " + configFile.getAbsolutePath());
        }

        Configuration configuration = new Configuration();

        if (configFile.getName().endsWith(".xml")) {
            configuration.configure(configFile);
        } else {
            try {
                Properties properties = PropertyDecrypter.getDecryptableProperties();
                properties.load(Files.newBufferedReader(configFile.toPath(), Charset.defaultCharset()));
                configuration.addProperties(properties);
                // Unwrap the decrypted property to let the connection pool framework see it
                // (as the connection pool framework reads properties using entryset iterators and jasypt EncryptableProperties does not override them)
                configuration.setProperty(PROP_HIBERNATE_CONNECTION_PASSWORD,
                                          properties.getProperty(PROP_HIBERNATE_CONNECTION_PASSWORD));
            } catch (IOException e) {
                throw new IllegalArgumentException(e);
            }
        }

        return new RMDBManager(configuration, drop, dropNS);
    }
}
 
Example 4
Source File: ConnectionManager.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a SessionFactory, loading the hbm.xml files from the specified
 * location.
 * @param packageNames Package name to be searched.
 */
private void createSessionFactory() {
    if (sessionFactory != null && !sessionFactory.isClosed()) {
        return;
    }

    List<String> hbms = new LinkedList<String>();

    for (Iterator<String> iter = packageNames.iterator(); iter.hasNext();) {
        String pn = iter.next();
        hbms.addAll(FinderFactory.getFinder(pn).find("hbm.xml"));
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found: " + hbms);
        }
    }

    try {
        Configuration config = new Configuration();
        /*
         * Let's ask the RHN Config for all properties that begin with
         * hibernate.*
         */
        LOG.info("Adding hibernate properties to hibernate Configuration");
        Properties hibProperties = Config.get().getNamespaceProperties(
                "hibernate");
        hibProperties.put("hibernate.connection.username",
                Config.get()
                .getString(ConfigDefaults.DB_USER));
        hibProperties.put("hibernate.connection.password",
                Config.get()
                .getString(ConfigDefaults.DB_PASSWORD));

        hibProperties.put("hibernate.connection.url",
                ConfigDefaults.get().getJdbcConnectionString());

        config.addProperties(hibProperties);

        for (Iterator<String> i = hbms.iterator(); i.hasNext();) {
            String hbmFile = i.next();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding resource " + hbmFile);
            }
            config.addResource(hbmFile);
        }
        if (configurators != null) {
            for (Iterator<Configurator> i = configurators.iterator(); i
                    .hasNext();) {
                Configurator c = i.next();
                c.addConfig(config);
            }
        }

        //TODO: Fix auto-discovery (see commit: e92b062)
        AnnotationRegistry.getAnnotationClasses().forEach(c -> {
            config.addAnnotatedClass(c);
        });

        // add empty varchar warning interceptor
        EmptyVarcharInterceptor interceptor = new EmptyVarcharInterceptor();
        interceptor.setAutoConvert(true);
        config.setInterceptor(interceptor);

        sessionFactory = config.buildSessionFactory();
    }
    catch (HibernateException e) {
        LOG.error("FATAL ERROR creating HibernateFactory", e);
    }
}
 
Example 5
Source File: ConnectionManager.java    From spacewalk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a SessionFactory, loading the hbm.xml files from the specified
 * location.
 * @param packageNames Package name to be searched.
 */
private void createSessionFactory() {
    if (sessionFactory != null && !sessionFactory.isClosed()) {
        return;
    }

    List<String> hbms = new LinkedList<String>();

    for (Iterator<String> iter = packageNames.iterator(); iter.hasNext();) {
        String pn = iter.next();
        hbms.addAll(FinderFactory.getFinder(pn).find("hbm.xml"));
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found: " + hbms);
        }
    }

    try {
        Configuration config = new Configuration();
        /*
         * Let's ask the RHN Config for all properties that begin with
         * hibernate.*
         */
        LOG.info("Adding hibernate properties to hibernate Configuration");
        Properties hibProperties = Config.get().getNamespaceProperties(
                "hibernate");
        hibProperties.put("hibernate.connection.username",
                Config.get()
                .getString(ConfigDefaults.DB_USER));
        hibProperties.put("hibernate.connection.password",
                Config.get()
                .getString(ConfigDefaults.DB_PASSWORD));

        hibProperties.put("hibernate.connection.url",
                ConfigDefaults.get().getJdbcConnectionString());

        config.addProperties(hibProperties);
        // Force the use of our txn factory
        if (config.getProperty(Environment.TRANSACTION_STRATEGY) != null) {
            throw new IllegalArgumentException("The property " +
                    Environment.TRANSACTION_STRATEGY +
                    " can not be set in a configuration file;" +
                    " it is set to a fixed value by the code");
        }

        for (Iterator<String> i = hbms.iterator(); i.hasNext();) {
            String hbmFile = i.next();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding resource " + hbmFile);
            }
            config.addResource(hbmFile);
        }
        if (configurators != null) {
            for (Iterator<Configurator> i = configurators.iterator(); i
                    .hasNext();) {
                Configurator c = i.next();
                c.addConfig(config);
            }
        }

        // add empty varchar warning interceptor
        EmptyVarcharInterceptor interceptor = new EmptyVarcharInterceptor();
        interceptor.setAutoConvert(true);
        config.setInterceptor(interceptor);

        sessionFactory = config.buildSessionFactory();
    }
    catch (HibernateException e) {
        LOG.error("FATAL ERROR creating HibernateFactory", e);
    }
}