Example usage for org.hibernate.cfg Configuration setProperty

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

Introduction

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

Prototype

public Configuration setProperty(String propertyName, String value) 

Source Link

Document

Set a property value by name

Usage

From source file:org.codehaus.griffon.runtime.hibernate3.internal.HibernateConfigurationHelper.java

License:Apache License

public Configuration buildConfiguration() {
    // Create Configuration instance.
    Configuration config = newConfiguration();

    dataSourceHolder.set(dataSource);/*  w w  w .j ava 2 s  .  c o m*/

    applyEntityInterceptor(config);
    applyNamingStrategy(config);
    applyProperties(config);
    applyDialect(config);
    applyMappings(config);

    Class providerClass = DataSourceConnectionProvider.class;
    // Set Spring-provided DataSource as Hibernate ConnectionProvider.
    config.setProperty(Environment.CONNECTION_PROVIDER, providerClass.getName());

    return config;
}

From source file:org.codehaus.griffon.runtime.hibernate3.internal.HibernateConfigurationHelper.java

License:Apache License

private void applyProperties(Configuration config) {
    Object props = getConfigValue(sessionConfig, PROPS, null);
    if (props instanceof Properties) {
        config.setProperties((Properties) props);
    } else if (props instanceof Map) {
        for (Map.Entry<String, String> entry : ((Map<String, String>) props).entrySet()) {
            config.setProperty(entry.getKey(), entry.getValue());
        }//from  w  w  w  .ja v  a2s.  c om
    }

    if (getConfigValueAsBoolean(sessionConfig, "logSql", false)) {
        config.setProperty("hibernate.show_sql", "true");
    }
    if (getConfigValueAsBoolean(sessionConfig, "formatSql", false)) {
        config.setProperty("hibernate.format_sql", "true");
    }
}

From source file:org.codehaus.griffon.runtime.hibernate3.internal.HibernateConfigurationHelper.java

License:Apache License

private void applyDialect(Configuration config) {
    Object dialect = getConfigValue(sessionConfig, "dialect", null);
    if (dialect instanceof Class) {
        config.setProperty("hibernate.dialect", ((Class) dialect).getName());
    } else if (dialect != null) {
        config.setProperty("hibernate.dialect", dialect.toString());
    } else {/*w  w  w.  j a  v  a2 s . co m*/
        DialectDetector dialectDetector = new DialectDetector(application, dataSource);
        config.setProperty("hibernate.dialect", dialectDetector.getDialect());
    }
}

From source file:org.codehaus.griffon.runtime.hibernate4.internal.HibernateConfigurationHelper.java

License:Apache License

private void applyDialect(Configuration config) {
    Object dialect = getConfigValue(sessionConfig, "dialect", null);
    if (dialect instanceof Class) {
        config.setProperty("hibernate.dialect", ((Class) dialect).getName());
    } else if (dialect != null) {
        config.setProperty("hibernate.dialect", dialect.toString());
    } else {/*from ww w.  jav  a 2s .  c  o m*/
        DialectDetector dialectDetector = new DialectDetector(dataSource);
        config.setProperty("hibernate.dialect", dialectDetector.getDialect());
    }
}

From source file:org.codehaus.griffon.runtime.hibernate5.internal.HibernateConfigurationHelper.java

License:Apache License

private void applyProviderClassToHibernate(Configuration config) {
    String providerClass = getConfigValueAsString(sessionConfig, PROVIDER_CLASS, null);
    if (providerClass != null) {
        config.setProperty("hibernate.connection.provider_class", providerClass);
        // Copying all connection configuration from data source to hibernate
        for (Map.Entry<String, String> keyValueConfiguration : dataSourceConfiguration.entrySet()) {
            if (groovyToHibernateConfigurationName.containsKey(keyValueConfiguration.getKey()))
                config.setProperty(groovyToHibernateConfigurationName.get(keyValueConfiguration.getKey()),
                        keyValueConfiguration.getValue());
        }/* w w  w .ja v  a 2s.  c o  m*/
    }

}

From source file:org.codehaus.griffon.runtime.hibernate5.internal.HibernateConfigurationHelper.java

License:Apache License

private void applySessionContext(Configuration config) {
    String sessionContext = getConfigValueAsString(sessionConfig, CURRENT_SESSION_CONTEXT, null);
    if (sessionContext != null)
        config.setProperty("hibernate.current_session_context_class", sessionContext);
}

From source file:org.codehaus.griffon.runtime.hibernate5.internal.HibernateConfigurationHelper.java

License:Apache License

private void applyProperties(Configuration config) {
    Object props = getConfigValue(sessionConfig, PROPS, null);
    if (props instanceof Properties) {
        config.setProperties((Properties) props);
    } else if (props instanceof Map) {
        for (Map.Entry<String, String> entry : ((Map<String, String>) props).entrySet()) {
            config.setProperty(entry.getKey(), entry.getValue());
        }/*from   ww w  . ja v a 2  s.  c  om*/
    }

    if (getConfigValueAsBoolean(sessionConfig, "logSql", false)) {
        config.setProperty("hibernate.show_sql", "true");
    }
    if (getConfigValueAsBoolean(sessionConfig, "formatSql", false)) {
        config.setProperty("hibernate.format_sql", "true");
    }

}

From source file:org.dynamise.sample.hibernate.DataAccessBundle.java

License:Apache License

@Signal(LifecycleEvent.START)
public void start(ServiceContext serviceCtx) {

    this.logger.info("Building session factory");

    // Pre-loading driver class
    String driverClass = this.properties.getString("db.driver");
    try {//from ww  w  .  j  a  va2 s  . c  o  m
        Class.forName(driverClass);
    } catch (ClassNotFoundException e) {
        throw new BundleException("Driver not found", e);
    }

    // Prepare config
    Configuration configuration = new Configuration();
    configuration.addAnnotatedClass(Book.class);
    configuration.setProperty("connection.driver_class", driverClass);
    configuration.setProperty("hibernate.connection.url", this.properties.getString("db.url"));
    configuration.setProperty("hibernate.connection.username", this.properties.getString("db.username"));
    configuration.setProperty("hibernate.connection.password", this.properties.getString("db.password"));
    configuration.setProperty("dialect", this.properties.getString("db.hDialect"));
    configuration.setProperty("hibernate.hbm2ddl.auto", this.properties.getString("db.hbm2ddl.auto"));

    try {

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        this.sessionFactory = configuration.buildSessionFactory(builder.build());

        serviceCtx.put(Constants.KEY_SESSION_FACTORY, this.sessionFactory);

    } catch (HibernateException e) {
        throw new BundleException(e);
    }

}

From source file:org.eclipse.emf.cdo.server.internal.hibernate.HibernateStore.java

License:Open Source License

protected void initDataStore() {
    if (TRACER.isEnabled()) {
        TRACER.trace("Initializing Configuration"); //$NON-NLS-1$
    }//  w w  w.  j a  va 2 s.c o m

    InputStream in = null;

    try {
        PackageRegistryProvider.getInstance().setThreadPackageRegistry(getRepository().getPackageRegistry());

        cdoDataStore = new CDODataStore();
        hibernateAuditHandler = new HibernateAuditHandler();
        hibernateAuditHandler.setCdoDataStore(cdoDataStore);
        hibernateAuditHandler.setHibernateStore(this);

        cdoDataStore.setResetConfigurationOnInitialization(false);
        cdoDataStore.setName("cdo");
        cdoDataStore.setPackageRegistry(getRepository().getPackageRegistry());
        cdoDataStore.getExtensionManager().registerExtension(EMFInterceptor.class.getName(),
                CDOInterceptor.class.getName());
        cdoDataStore.getExtensionManager().registerExtension(AuditHandler.class.getName(),
                CDOAuditHandler.class.getName());
        cdoDataStore.getExtensionManager().registerExtension(AuditProcessHandler.class.getName(),
                CDOAuditProcessHandler.class.getName());

        // don't do any persistence xml mapping in this datastore
        // make a local copy as it is adapted in the next if-statement
        // and we want to keep the original one untouched, if not
        // subsequent test runs will fail as they use the same
        // properties object
        final Properties props = new Properties();
        props.putAll(getProperties());
        props.remove(PersistenceOptions.PERSISTENCE_XML);

        if (!props.containsKey(PersistenceOptions.HANDLE_UNSET_AS_NULL)) {
            props.setProperty(PersistenceOptions.HANDLE_UNSET_AS_NULL, "true");
        }

        cdoDataStore.setDataStoreProperties(props);
        Configuration hibernateConfiguration = cdoDataStore.getConfiguration();

        if (mappingProvider != null) {
            mappingProvider.setHibernateStore(this);
            mappingXml = mappingProvider.getMapping();
            hibernateConfiguration.addXML(mappingXml);
        }

        if (TRACER.isEnabled()) {
            TRACER.trace("Adding resource.hbm.xml to configuration"); //$NON-NLS-1$
        }

        in = OM.BUNDLE.getInputStream(RESOURCE_HBM_PATH);
        hibernateConfiguration.addInputStream(in);
        // hibernateConfiguration.setInterceptor(new CDOInterceptor());

        hibernateConfiguration.setProperties(props);

        // prevent the drop on close because the sessionfactory is also closed when
        // new packages are written to the db, so only do a real drop at deactivate
        if (hibernateConfiguration.getProperty(Environment.HBM2DDL_AUTO) != null
                && hibernateConfiguration.getProperty(Environment.HBM2DDL_AUTO).startsWith(HBM2DLL_CREATE)) {
            doDropSchema = true;
            // note that the value create also re-creates the db and drops the old one
            hibernateConfiguration.setProperty(Environment.HBM2DDL_AUTO, HBM2DLL_UPDATE);
            cdoDataStore.getDataStoreProperties().setProperty(Environment.HBM2DDL_AUTO, HBM2DLL_UPDATE);
        } else {
            doDropSchema = false;
        }

        final List<EPackage> ePackages = new ArrayList<EPackage>(packageHandler.getEPackages());

        // get rid of the system packages
        for (EPackage ePackage : packageHandler.getEPackages()) {
            if (CDOModelUtil.isSystemPackage(ePackage) && ePackage != EtypesPackage.eINSTANCE) {
                ePackages.remove(ePackage);
            }
        }
        // remove the persistence xml if no epackages as this won't work without
        // epackages
        if (ePackages.size() == 0 && props.getProperty(PersistenceOptions.PERSISTENCE_XML) != null) {
            cdoDataStore.getDataStoreProperties().remove(PersistenceOptions.PERSISTENCE_XML);
        }

        if (isAuditing()) {
            auditEPackages = createAuditEPackages(cdoDataStore);
            final String auditMapping = mapAuditingEPackages(cdoDataStore, auditEPackages);
            // System.err.println(auditMapping);
            hibernateConfiguration.addXML(auditMapping);
            cdoDataStore.setAuditing(true);
        }
        cdoDataStore.setEPackages(ePackages.toArray(new EPackage[0]));
    } catch (Exception ex) {
        throw WrappedException.wrap(ex);
    } finally {
        PackageRegistryProvider.getInstance().setThreadPackageRegistry(null);
        IOUtil.close(in);
    }
}

From source file:org.exoplatform.services.organization.idm.DisabledUserMigrationScript.java

License:Open Source License

private void setupHibernate(Properties config) throws Exception {
    final Configuration conf_ = new Configuration();

    conf_.setProperty("hibernate.connection.driver_class",
            config.getProperty("hibernate.connection.driver_class"));
    conf_.setProperty("hibernate.connection.url", config.getProperty("hibernate.connection.url"));
    conf_.setProperty("hibernate.connection.username", config.getProperty("hibernate.connection.username"));
    conf_.setProperty("hibernate.connection.password", config.getProperty("hibernate.connection.password"));
    conf_.setProperty("hibernate.dialect", config.getProperty("hibernate.dialect"));

    String config_path = config.getProperty("hibernate.config_path");
    URL url = Thread.currentThread().getContextClassLoader().getResource(config_path);
    if (url == null) {
        log.error("hibernate config file not found: {}", config_path);
    } else {//from   w ww .  j  a  v  a 2s .  c o  m
        log.info("adding hibernate config file {}", config_path);
        conf_.addURL(url);
    }

    sessionFactory_ = SecurityHelper.doPrivilegedAction(new PrivilegedAction<SessionFactory>() {
        public SessionFactory run() {
            SessionFactory factory = conf_.configure().buildSessionFactory();
            return factory;
        }
    });
}