Example usage for org.hibernate.cfg Configuration setProperties

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

Introduction

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

Prototype

public Configuration setProperties(Properties properties) 

Source Link

Document

Specify a completely new set of properties

Usage

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  v a2  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 {// ww  w.  j  a va 2  s. com
            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  ww  w.  ja v a2s  . 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;
}

From source file:org.opendds.jms.persistence.HibernatePersistenceManager.java

License:Open Source License

public HibernatePersistenceManager(Properties properties) {
    Configuration cfg = new Configuration();

    for (Class persistentClass : getPersistentClasses()) {
        cfg.addClass(persistentClass);//from w w  w.ja  v  a2  s .  com
    }
    cfg.setProperties(properties);

    SessionFactory sessionFactory = cfg.buildSessionFactory();

    // Initialize persistence stores
    durableSubscriptionStore = new DurableSubscriptionStore(sessionFactory);
}

From source file:org.opentides.util.HibernateUtil.java

License:Apache License

/**
 * Static initializer to establish database connection.
 *//*from   www  . ja  va2  s  . co m*/
private static void initialize() {
    try {
        URL[] urls = ClasspathUrlFinder.findResourceBases(persistenceFile);
        List<URL> toAdd = new ArrayList<URL>();
        List<URL> finalUrls = new ArrayList<URL>();
        for (URL url : urls) {
            LOGGER.debug("URL is : [" + url.getPath() + "] with protocol: [" + url.getProtocol()
                    + "] with file: [" + url.getFile() + "]");
            String file = url.getFile();
            if (!file.startsWith("file:")) {
                file = "file:" + file;
            }
            if ("zip".equals(url.getProtocol())) {
                toAdd.add(new URL("jar", url.getHost(), url.getPort(), file));
            } else {
                toAdd.add(url);
            }
        }
        finalUrls.addAll(toAdd);
        for (URL url : finalUrls) {
            LOGGER.debug("URL full path: [" + url.toString() + "]");
            LOGGER.debug("Final URL is : [" + url.getPath() + "] with protocol: [" + url.getProtocol()
                    + "] with file: [" + url.getFile() + "]");
        }
        AnnotationDB db = new AnnotationDB();
        db.scanArchives(finalUrls.toArray(new URL[finalUrls.size()]));
        Set<String> entityClasses = db.getAnnotationIndex().get(javax.persistence.Entity.class.getName());
        // Create the SessionFactory
        Configuration ac = new Configuration();
        Properties properties = XMLPersistenceUtil.getProperties(persistenceFile, persistenceUnitName);
        ac.setProperties(properties);
        if (StringUtil.isEmpty(jndiName)) {
            ac.setProperty("hibernate.connection.driver_class", driverClassName);
            ac.setProperty("hibernate.connection.url", url);
            ac.setProperty("hibernate.connection.username", username);
            ac.setProperty("hibernate.connection.password", password);
        } else {
            LOGGER.debug("JNDI not empty. [" + jndiName + "] using persistence [" + persistenceFile + "]");
            ac.setProperty("hibernate.connection.datasource", jndiName);
        }
        for (String clazz : entityClasses) {
            LOGGER.debug("Entity Class : [" + clazz + "]");
            ac.addAnnotatedClass(Class.forName(clazz));
        }
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.AuditLog"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.BaseEntity"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.FileInfo"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.SystemCodes"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.Widget"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.UserWidgets"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.user.BaseUser"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.PasswordReset"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.user.UserRole"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.user.UserCredential"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.user.UserGroup"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.UserDefinedRecord"));
        ac.addAnnotatedClass(Class.forName("org.opentides.bean.UserDefinedField"));
        sessionFactory = ac.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.siberia.binding.impl.db.hibernate.HibernateBindingManager.java

License:Open Source License

/** method that returns the configuration to use
 *  @return a Configuration//w  w  w.  j a v a 2s .c  o m
 */
private Configuration createConfiguration() {
    Configuration cfg = new Configuration();

    List<Class> classes = TypeInformationProvider.getInstance().getSubClassFor(SibType.class, true, true, true,
            (Class[]) null);

    if (classes != null) {
        String suffix = ".hbm.xml";

        for (int i = 0; i < classes.size(); i++) {
            Class current = classes.get(i);

            if (current != null) {
                String pluginId = TypeInformationProvider.getInstance().getPluginDeclaring(current);

                if (pluginId == null) {
                    logger.warn("could not find the plugin declaring class " + current);
                } else {
                    StringBuffer buffer = new StringBuffer(current.getName().length() + suffix.length());

                    buffer.append(current.getName());

                    /** replace . by / */
                    for (int j = 0; j < buffer.length(); j++) {
                        char currentChar = buffer.charAt(j);

                        if (currentChar == '.') {
                            buffer.deleteCharAt(j);
                            buffer.insert(j, '/');
                        }
                    }
                    buffer.append(suffix);

                    URL url = ResourceLoader.getInstance().getPluginClassLoader(pluginId)
                            .getResource(buffer.toString());

                    if (url == null) {
                        logger.warn("resource " + buffer.toString() + " could not be found --> ignored");
                    } else {
                        logger.info("adding hibernate mapping declaration from " + url);
                        cfg = cfg.addURL(url);
                    }
                }
            }
        }
    }

    /** configure Configuration properties */

    Properties convertedProperties = this.convertProperties(this.getProperties());

    this.reportIgnoredProperties();

    Enumeration en = convertedProperties.keys();
    while (en.hasMoreElements()) {
        Object current = en.nextElement();

        logger.debug(
                "consider database property '" + current + "' --> '" + convertedProperties.get(current) + "'");
    }

    cfg = cfg.setProperties(convertedProperties);

    return cfg;
}

From source file:org.yawlfoundation.yawl.elements.data.external.HibernateEngine.java

License:Open Source License

public void configureSession(Properties props, List<Class> classes) {
    Configuration cfg = new Configuration();
    cfg.setProperties(props);

    if (classes != null) {
        for (Class className : classes) {
            cfg.addClass(className);/*from  w w  w .  j  ava  2  s  . com*/
        }
    }

    _factory = cfg.buildSessionFactory(); // get a session context        

    // check tables exist and are of a matching format to the persisted objects
    new SchemaUpdate(cfg).execute(false, true);
}

From source file:org.yawlfoundation.yawl.util.HibernateEngine.java

License:Open Source License

/** initialises hibernate and the required tables */
private void initialise(Set<Class> classes, Properties props) throws HibernateException {
    try {//  ww w  . ja  va  2 s  .  c  o  m
        Configuration _cfg = new Configuration();

        // if props supplied, use them instead of hibernate.properties
        if (props != null) {
            _cfg.setProperties(props);
        }

        // add each persisted class to config
        for (Class persistedClass : classes) {
            _cfg.addClass(persistedClass);
        }

        // get a session context
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(_cfg.getProperties()).build();
        _factory = _cfg.buildSessionFactory(serviceRegistry);

        // check tables exist and are of a matching format to the persisted objects
        new SchemaUpdate(_cfg).execute(false, true);

    } catch (MappingException me) {
        _log.error("Could not initialise database connection.", me);
    }
}

From source file:sk.lazyman.gizmo.SpringApplicationContextTest.java

License:Apache License

private void createSQLSchema(String fileName) throws Exception {
    org.hibernate.cfg.Configuration configuration = new Configuration();
    Properties properties = new Properties();
    properties.putAll(sessionFactoryBean.getJpaPropertyMap());
    configuration.setProperties(properties);
    configuration.setNamingStrategy(new GizmoNamingStrategy());

    System.out.println("Dialect: " + properties.getProperty("hibernate.dialect"));

    addAnnotatedClasses("sk.lazyman.gizmo.data", configuration);

    SchemaExport export = new SchemaExport(configuration);
    export.setOutputFile(fileName);/*from   ww  w.ja va2s . com*/
    export.setDelimiter(";");
    export.execute(true, false, false, true);
}

From source file:uk.ac.ebi.metabolights.repository.dao.hibernate.DAOTest.java

License:Apache License

@Before
public void setUp() throws Exception {

    Configuration configuration = new Configuration();

    // Get property file
    Properties hibernateProperties = new Properties();
    hibernateProperties.load(DAOTest.class.getResourceAsStream("/hibernate.hidden.properties"));

    configuration.setProperties(hibernateProperties);

    initFolders();/*from   w ww  . j  ava  2  s .c om*/

    DAOFactory.initialize(configRoot, studiesLocation, configuration);

    //cleanDB();

}