Example usage for org.hibernate.cfg Configuration getProperty

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

Introduction

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

Prototype

public String getProperty(String propertyName) 

Source Link

Document

Get a property value by name

Usage

From source file:org.jadira.usertype.spi.shared.AbstractUserTypeHibernateIntegrator.java

License:Apache License

public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {

    try {//ww  w  .  j  a va 2 s .com
        ConfigurationHelper.setCurrentSessionFactory(sessionFactory);

        String isEnabled = configuration.getProperty(REGISTER_USERTYPES_KEY);
        String javaZone = configuration.getProperty(DEFAULT_JAVAZONE_KEY);
        String databaseZone = configuration.getProperty(DEFAULT_DATABASEZONE_KEY);
        String seed = configuration.getProperty(DEFAULT_SEED_KEY);
        String currencyCode = configuration.getProperty(DEFAULT_CURRENCYCODE_KEY);

        String jdbc42Apis = configuration.getProperty(JDBC42_API_KEY);

        configureDefaultProperties(sessionFactory, javaZone, databaseZone, seed, currencyCode, jdbc42Apis);

        if (isEnabled != null && Boolean.valueOf(isEnabled)) {
            autoRegisterUsertypes(configuration);
        }

        final boolean use42Api = use42Api(configuration.getProperty(JDBC42_API_KEY), sessionFactory);
        ConfigurationHelper.setUse42Api(sessionFactory, use42Api);

        // doIntegrate(configuration, sessionFactory, serviceRegistry);
    } finally {
        ConfigurationHelper.setCurrentSessionFactory(null);
    }
}

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

License:Open Source License

public Configuration createConfiguration(Configuration localCfg, boolean includeMappings) {
    Properties properties = prefs.getProperties();

    if (properties != null) {
        // in case the transaction manager is empty then we need to inject a faketm since
        // hibernate will still try and instantiate it.
        String str = properties.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY);
        if (str != null && StringHelper.isEmpty(str)) {
            properties.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, FAKE_TM_LOOKUP);
            // properties.setProperty( "hibernate.transaction.factory_class", "");
        }/*from   w  ww  .ja v a2 s  .  c om*/
    }
    if (localCfg == null) {
        localCfg = buildConfiguration(properties, includeMappings);
    } else {
        // Properties origProperties = cfg.getProperties();
        // origProperties.putAll(properties);
        // cfg.setProperties(origProperties);
        // TODO: this is actually only for jdbc reveng...
        // localCfg = configureStandardConfiguration( includeMappings, localCfg, properties );
    }

    // here both setProperties and configxml have had their chance to tell which databasedriver
    // is needed.
    registerFakeDriver(localCfg.getProperty(Environment.DRIVER));
    // autoConfigureDialect(localCfg); Disabled for now since it causes very looong timeouts for
    // non-running databases + i havent been needed until now...

    // TODO: jpa configuration ?
    if (includeMappings) {
        File[] mappingFiles = prefs.getMappingFiles();

        for (int i = 0; i < mappingFiles.length; i++) {
            File hbm = mappingFiles[i];
            localCfg = localCfg.addFile(hbm);
        }
    }
    // TODO: HBX-
    localCfg.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false"); //$NON-NLS-1$//$NON-NLS-2$
    localCfg.setProperty(Environment.HBM2DDL_AUTO, "false"); //$NON-NLS-1$
    // to fix: JBIDE-5839 & JBIDE-5997 - setup this property: false is default value
    // to make hibernate tools diff hibernate versions compatible:
    // if the property not set get NoSuchMethodError with FullTextIndexEventListener
    if (localCfg.getProperty("hibernate.search.autoregister_listeners") == null) { //$NON-NLS-1$
        localCfg.setProperty("hibernate.search.autoregister_listeners", "false"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return localCfg;
}

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

License:Open Source License

@SuppressWarnings("unused")
private void autoConfigureDialect(Configuration localCfg) {
    if (localCfg.getProperty(Environment.DIALECT) == null) {
        String dialect = ConnectionProfileUtil.autoDetectDialect(localCfg.getProperties());
        if (dialect != null) {
            localCfg.setProperty(Environment.DIALECT, dialect);
        }/*  w  w  w. ja va2 s . c o  m*/
    }
}

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

License:Open Source License

private Configuration configureStandardConfiguration(final boolean includeMappings, Configuration localCfg,
        Properties properties) {//from w w w.  j  av  a2 s . c  o m
    if (properties != null) {
        localCfg = localCfg.setProperties(properties);
    }
    EntityResolver entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER;
    if (StringHelper.isNotEmpty(prefs.getEntityResolverName())) {
        try {
            entityResolver = (EntityResolver) ReflectHelper.classForName(prefs.getEntityResolverName())
                    .newInstance();
        } catch (Exception c) {
            throw new HibernateConsoleRuntimeException(
                    ConsoleMessages.ConsoleConfiguration_could_not_configure_entity_resolver
                            + prefs.getEntityResolverName(),
                    c);
        }
    }
    localCfg.setEntityResolver(entityResolver);
    if (StringHelper.isNotEmpty(prefs.getNamingStrategy())) {
        try {
            NamingStrategy ns = (NamingStrategy) ReflectHelper.classForName(prefs.getNamingStrategy())
                    .newInstance();
            localCfg.setNamingStrategy(ns);
        } catch (Exception c) {
            throw new HibernateConsoleRuntimeException(
                    ConsoleMessages.ConsoleConfiguration_could_not_configure_naming_strategy
                            + prefs.getNamingStrategy(),
                    c);
        }
    }
    localCfg = loadConfigurationXML(localCfg, includeMappings, entityResolver);
    changeDatasourceProperties(localCfg);
    localCfg = configureConnectionProfile(localCfg);
    // replace dialect if it is set in preferences
    if (StringHelper.isNotEmpty(prefs.getDialectName())) {
        localCfg.setProperty(Environment.DIALECT, prefs.getDialectName());
    }
    if (StringHelper.isEmpty(localCfg.getProperty("javax.persistence.validation.mode"))) {//$NON-NLS-1$
        localCfg.setProperty("javax.persistence.validation.mode", "none"); //$NON-NLS-1$//$NON-NLS-2$
    }
    return localCfg;
}

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

License:Open Source License

@SuppressWarnings("unused")
private void autoConfigureDialect(Configuration localCfg, ServiceRegistry serviceRegistry) {
    if (localCfg.getProperty(Environment.DIALECT) == null) {
        String dialect = ConnectionProfileUtil.autoDetectDialect(localCfg.getProperties());
        if (dialect != null) {
            localCfg.setProperty(Environment.DIALECT, dialect);
        }//from  w  w  w. ja  v a2 s  .  c om
    }
}

From source file:org.jbpm.pvm.internal.wire.binding.TransactionBinding.java

License:Open Source License

protected String autoDetectHibernateConfiguration(String propertyName) {
    Configuration cfg = EnvironmentImpl.getFromCurrent(Configuration.class, false);
    if (cfg == null) {
        return null;
    }/*from w  w w . ja  v a 2 s .c om*/
    return cfg.getProperty(propertyName);
}

From source file:org.jpos.ee.support.JPosHibernateConfiguration.java

License:Open Source License

private void configureMappings(Configuration cfg) throws ConfigurationException, IOException {
    try {//from  ww  w.  j a v a2 s.  co  m
        //Read DB properties.
        String propFile = cfg.getProperty(DB_PROPERTY_FILE);
        propFile = propFile == null ? "cfg/db.properties" : propFile;
        Properties dbProps = loadProperties(propFile);
        if (dbProps != null) {
            cfg.addProperties(dbProps);
        }

        SAXReader reader = new SAXReader();

        List<String> moduleConfigs = ModuleUtils.getModuleEntries("META-INF/org/jpos/ee/modules/");
        for (String moduleConfig : moduleConfigs) {
            final URL url = getClass().getClassLoader().getResource(moduleConfig);
            final Document doc = reader.read(url);
            Element mappings = doc.getRootElement().element("mappings");
            if (mappings != null) {
                addMappings(cfg, mappings, moduleConfig);
            }
        }
    } catch (DocumentException e) {
        throw new ConfigurationException("Could not parse mappings document", e);
    }
}

From source file:org.lingcloud.molva.ocl.util.HibernateUtil.java

License:Apache License

/**
 * Rebuild the SessionFactory with the given Hibernate Configuration.
 * <p>/*from  ww w .  jav  a2 s  .c om*/
 * HibernateUtil does not configure() the given Configuration object, it
 * directly calls buildSessionFactory(). This method also closes the old
 * static variable SessionFactory before, if it is still open.
 * 
 * @param cfg
 */
public void rebuildSessionFactory(Configuration cfg) {
    log.debug("Rebuilding the SessionFactory from given Configuration");
    initConfiguration();

    if (sessionFactory != null && !sessionFactory.isClosed()) {
        sessionFactory.close();
    }
    if (cfg.getProperty(Environment.SESSION_FACTORY_NAME) != null) {
        log.debug("Managing SessionFactory in JNDI");
        cfg.buildSessionFactory();
    } else {
        log.debug("Holding SessionFactory in static variable");
        sessionFactory = cfg.buildSessionFactory();
    }
    configuration = cfg;
}

From source file:org.openspaces.persistency.hibernate.SessionFactoryBuilder.java

License:Open Source License

/**
 * Create and configure new hibernate session factory
 *
 * @return SessionFactory/*  w w  w .  ja  va 2s .  c o m*/
 */
public static SessionFactory getFactory(String hibernateFile) throws Exception {
    Configuration config = null;

    try {
        // load the class using reflection to avoid JIT exceptions  
        config = configure(
                (Configuration) ClassLoaderHelper.loadClass(ANNOTATION_CONFIGURATION_CLASS).newInstance(),
                hibernateFile);
    } catch (Throwable t) {
        try {
            config = configure(new Configuration(), hibernateFile);
        } catch (Exception e) {
            // if both methods failed - log first exception
            // and throw the second
            if (_logger.isLoggable(Level.SEVERE)) {
                _logger.log(Level.SEVERE, "Failed to configure using hibernate annotations.", t);

            }

            throw e;
        }
    }
    // since hibernate doesn't support configuring naming strategies in cfg.xml.
    // added an option to configure it programatically while using the hibernate.cfg.xml
    // for example: add this to hibernate.cfg.xml
    //<property name="hibernate.naming_strategy">com.gigaspaces.test.persistent.SpaceNamingStrategy</property>

    String namingStrategyClass = config.getProperty(HIBERNATE_NAMING_STRATEGY);

    if (namingStrategyClass != null) {
        NamingStrategy namingStrategy = (NamingStrategy) ClassLoaderHelper.loadClass(namingStrategyClass)
                .newInstance();
        config.setNamingStrategy(namingStrategy);
    }
    return config.buildSessionFactory();
}

From source file:org.oursight.framework.yao.base.data.dao.impl.Hb3Util.java

License:Open Source License

/**
 * JDBC.<BR>/*from www  .ja v  a 2 s .  c  o  m*/
 * @param hbCfg The Hibernate Configuration Object
 * @return JDBC
 */
static String getDBDriver(Configuration hbCfg) {
    return hbCfg.getProperty(Environment.DRIVER);
}