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.unitils.orm.hibernate.HibernateModuleConfigurationInheritanceTest.java

License:Apache License

/**
 * Test reusing a configuration of a super class.
 *///from  w w  w .java 2 s.co m
@Test
public void testGetHibernateConfiguration_twice() {
    Configuration hibernateConfiguration1 = hibernateModule
            .getConfigurationObject(new HibernateTestNoCreation1());
    Configuration hibernateConfiguration2 = hibernateModule
            .getConfigurationObject(new HibernateTestNoCreation2());

    assertNotNull(hibernateConfiguration1);
    assertEquals("org/unitils/orm/hibernate/hibernate.cfg.xml", hibernateConfiguration1.getProperty("name"));
    assertSame(hibernateConfiguration1, hibernateConfiguration2);
}

From source file:org.unitils.orm.hibernate.HibernateModuleConfigurationTest.java

License:Apache License

/**
 * Tests loading of a configuration location specified on class-level.
 *//*from w  w  w  . j  a  v a  2 s .co  m*/
@Test
public void testGetHibernateConfiguration_classLevel() {
    HibernateTestClassLevel hibernateTestClassLevel = new HibernateTestClassLevel();
    Configuration hibernateConfiguration = hibernateModule.getConfigurationObject(hibernateTestClassLevel);

    assertNotNull(hibernateConfiguration);
    assertEquals("org/unitils/orm/hibernate/hibernate.cfg.xml", hibernateConfiguration.getProperty("name"));
}

From source file:org.unitils.orm.hibernate.HibernateModuleConfigurationTest.java

License:Apache License

/**
 * Tests loading of a configuration location specified on field-level.
 *///w w  w  . ja  v  a2 s  .c o m
@Test
public void testGetHibernateConfiguration_fieldLevel() {
    HibernateTestFieldLevel hibernateTestFieldLevel = new HibernateTestFieldLevel();
    Configuration hibernateConfiguration = hibernateModule.getConfigurationObject(hibernateTestFieldLevel);

    assertNotNull(hibernateConfiguration);
    assertEquals("org/unitils/orm/hibernate/hibernate.cfg.xml", hibernateConfiguration.getProperty("name"));
}

From source file:org.unitils.orm.hibernate.HibernateModuleConfigurationTest.java

License:Apache License

/**
 * Tests loading of a configuration location specified on field-level.
 */// w w w .  j a v  a 2  s .c  om
@Test
public void testGetHibernateConfiguration_setterLevel() {
    HibernateTestSetterLevel hibernateTestSetterLevel = new HibernateTestSetterLevel();
    Configuration hibernateConfiguration = hibernateModule.getConfigurationObject(hibernateTestSetterLevel);

    assertNotNull(hibernateConfiguration);
    assertEquals("org/unitils/orm/hibernate/hibernate.cfg.xml", hibernateConfiguration.getProperty("name"));
}

From source file:org.unitils.orm.hibernate.HibernateModuleConfigurationTest.java

License:Apache License

/**
 * Test reusing a configuration for the same class.
 *//*from www  . j ava 2 s.  c  o  m*/
@Test
public void testGetHibernateConfiguration_twice() {
    Configuration hibernateConfiguration1 = hibernateModule
            .getConfigurationObject(new HibernateTestClassLevel());
    Configuration hibernateConfiguration2 = hibernateModule
            .getConfigurationObject(new HibernateTestClassLevel());

    assertNotNull(hibernateConfiguration1);
    assertEquals("org/unitils/orm/hibernate/hibernate.cfg.xml", hibernateConfiguration1.getProperty("name"));
    assertSame(hibernateConfiguration1, hibernateConfiguration2);
}

From source file:org.unitils.orm.jpa.util.provider.hibernate.HibernateJpaProviderSupport.java

License:Apache License

/**
 * Gets the database dialect from the Hibernate <code>Ejb3Configuration</code.
 *
 * @param configuration The hibernate config, not null
 * @return the database Dialect, not null
 *//*w  w w .ja v  a  2s. co  m*/
protected Dialect getHibernateDatabaseDialect(Configuration configuration) {
    String dialectClassName = configuration.getProperty("hibernate.dialect");
    if (isEmpty(dialectClassName)) {
        throw new UnitilsException("Property hibernate.dialect not specified");
    }
    try {
        return (Dialect) Class.forName(dialectClassName).newInstance();
    } catch (Exception e) {
        throw new UnitilsException("Could not instantiate dialect class " + dialectClassName, e);
    }
}

From source file:org.unitime.commons.hibernate.id.UniqueIdGenerator.java

License:Open Source License

public static void configure(Configuration config) {
    sGenClass = config.getProperty("tmtbl.uniqueid.generator");
    if (sGenClass == null)
        sGenClass = "org.hibernate.id.SequenceGenerator";
    sDefaultSchema = config.getProperty("default_schema");
    sNormalizer = config.createMappings().getObjectNameNormalizer();
}

From source file:org.unitime.commons.hibernate.util.HibernateUtil.java

License:Open Source License

public static void fixSchemaInFormulas(Configuration cfg) {
    cfg.buildMappings();// w  w  w.  j a v a2  s  .c  om
    String schema = cfg.getProperty("default_schema");
    if (schema != null) {
        for (Iterator i = cfg.getClassMappings(); i.hasNext();) {
            PersistentClass pc = (PersistentClass) i.next();
            for (Iterator j = pc.getPropertyIterator(); j.hasNext();) {
                Property p = (Property) j.next();
                for (Iterator k = p.getColumnIterator(); k.hasNext();) {
                    Selectable c = (Selectable) k.next();
                    if (c instanceof Formula) {
                        Formula f = (Formula) c;
                        if (f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%") >= 0) {
                            f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
                            sLog.debug("Schema updated in " + pc.getClassName() + "." + p.getName() + " to "
                                    + f.getFormula());
                        }
                    }
                }
            }
        }
    }
}

From source file:org.unitime.commons.hibernate.util.HibernateUtil.java

License:Open Source License

public static void configureHibernateFromRootDAO(String cfgName, Configuration cfg) {
    try {//from  w ww  . j  a  v a 2  s .c  om
        EntityResolver entityResolver = new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) {
                if (publicId.equals("-//Hibernate/Hibernate Mapping DTD 3.0//EN")) {
                    return new InputSource(HibernateUtil.class.getClassLoader()
                            .getResourceAsStream("org/hibernate/hibernate-mapping-3.0.dtd"));
                } else if (publicId.equals("-//Hibernate/Hibernate Mapping DTD//EN")) {
                    return new InputSource(HibernateUtil.class.getClassLoader()
                            .getResourceAsStream("org/hibernate/hibernate-mapping-3.0.dtd"));
                } else if (publicId.equals("-//Hibernate/Hibernate Configuration DTD 3.0//EN")) {
                    return new InputSource(HibernateUtil.class.getClassLoader()
                            .getResourceAsStream("org/hibernate/hibernate-configuration-3.0.dtd"));
                } else if (publicId.equals("-//Hibernate/Hibernate Configuration DTD//EN")) {
                    return new InputSource(HibernateUtil.class.getClassLoader()
                            .getResourceAsStream("org/hibernate/hibernate-configuration-3.0.dtd"));
                }
                return null;
            }
        };

        cfg.setEntityResolver(entityResolver);
        sLog.debug("  -- added entity resolver");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        sLog.debug("  -- document factory created");
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        sLog.debug("  -- document builder created");
        Document document = builder
                .parse(ConfigHelper.getConfigStream(cfgName == null ? "hibernate.cfg.xml" : cfgName));

        String dialect = ApplicationProperty.DatabaseDialect.value();
        if (dialect != null)
            setProperty(document, "dialect", dialect);

        String default_schema = ApplicationProperty.DatabaseSchema.value();
        if (default_schema != null)
            setProperty(document, "default_schema", default_schema);

        String idgen = ApplicationProperty.DatabaseUniqueIdGenerator.value();
        if (idgen != null)
            setProperty(document, "tmtbl.uniqueid.generator", idgen);

        if (ApplicationProperty.HibernateClusterEnabled.isFalse())
            setProperty(document, "net.sf.ehcache.configurationResourceName", "ehcache-nocluster.xml");

        for (Enumeration e = ApplicationProperties.getProperties().propertyNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            if (name.startsWith("hibernate.") || name.startsWith("connection.")
                    || name.startsWith("tmtbl.hibernate.")) {
                String value = ApplicationProperties.getProperty(name);
                if ("NULL".equals(value))
                    removeProperty(document, name);
                else
                    setProperty(document, name, value);
                if (!name.equals("connection.password"))
                    sLog.debug("  -- set " + name + ": " + value);
                else
                    sLog.debug("  -- set " + name + ": *****");
            }
        }

        cfg.configure(document);
        sLog.debug("  -- hibernate configured");

        HibernateUtil.fixSchemaInFormulas(cfg);
        sLog.debug("  -- %SCHEMA% in formulas changed to " + cfg.getProperty("default_schema"));

        UniqueIdGenerator.configure(cfg);
        sLog.debug("  -- UniquId generator configured");
    } catch (Exception e) {
        sLog.error("Unable to configure hibernate, reason: " + e.getMessage(), e);
    }
}

From source file:org.wildfly.extras.db_bootstrap.DbBootstrapScanDetectorProcessor.java

License:Apache License

/**
 * Loads all <code>dbbootstrap.[user-space-cfg-name-here].[hibernate-property-name-here]</code> properties from system
 * properties. <br>//from   w w  w.  ja  v a  2  s  .com
 * <br>
 * Any existing hibernate properties with the same name (<code>[hibernate-property-name-here]</code> in above example) will
 * be replaced by the matching system property. <br>
 * <br>
 *
 * @param bootstrapDatabaseAnnotation - bootstrap configuration source
 * @param configuration - the runtime hibernate configuration object
 */
private void configureSettingsFromSystemProperties(BootstrapDatabase bootstrapDatabaseAnnotation,
        Configuration configuration) {
    String propertyPrefix = String.format("%s.%s", DBBOOTSTRAP_SYSTEM_PROPERTY_PREFIX,
            bootstrapDatabaseAnnotation.name());
    DbBootstrapLogger.ROOT_LOGGER.tracef(
            "Searching for system properties with prefix %s to set and/or override hibernate configuration properties",
            propertyPrefix);
    for (Entry<Object, Object> entrySet : (System.getProperties().entrySet())) {
        if (entrySet.getKey().toString().startsWith(propertyPrefix)) {
            String hibernatePropertyName = entrySet.getKey().toString()
                    .replace(String.format("%s.", propertyPrefix), "");
            String oldHibernatePropertyValue = (configuration.getProperty(hibernatePropertyName) == null)
                    ? " (New property)"
                    : String.format(" (Replacing existing property with old value=%s)",
                            configuration.getProperty(hibernatePropertyName));
            String newHibernatePropertyValue = entrySet.getValue().toString();
            DbBootstrapLogger.ROOT_LOGGER.tracef("Setting hibernate property: %s=%s%s", hibernatePropertyName,
                    newHibernatePropertyValue, oldHibernatePropertyValue);
            configuration.setProperty(hibernatePropertyName, newHibernatePropertyValue);
        }
    }
}