Example usage for org.hibernate.cfg Configuration addSqlFunction

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

Introduction

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

Prototype

public void addSqlFunction(String functionName, SQLFunction function) 

Source Link

Usage

From source file:edu.ku.brc.dbsupport.HibernateUtil.java

License:Open Source License

/**
 * Sets up the hibernate configured params
 * // w w w .  j a v  a 2s . c o  m
 * @param config the config object
 */
public static void setHibernateLogonConfig(final Configuration config) {
    DBConnection dbConn = DBConnection.getInstance();

    String userName = dbConn.getUserName();
    String password = dbConn.getPassword();
    String driver = dbConn.getDriver();

    String connection = dbConn.getConnectionStr();

    // I commented this out so the property in hibernate.cfg.xml could work.
    // A better way to show SQL output is to put the following line in
    // the hibernate.cfg.xml file.
    // <property name="hibernate.show_sql">true</property>
    //config.setProperty("hibernate.show_sql", "false");

    config.setProperty("hibernate.connection.username", userName); //$NON-NLS-1$
    config.setProperty("hibernate.connection.password", password); //$NON-NLS-1$

    config.setProperty("hibernate.connection.autoReconnect", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("hibernate.connection.autoReconnectForPools", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("hibernate.connection.is-connection-validation-required", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    config.setProperty("connection.autoReconnect", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("connection.autoReconnectForPools", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    config.setProperty("connection.is-connection-validation-required", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    log.info("Using database [" + connection + "]"); //$NON-NLS-1$ //$NON-NLS-2$

    // if not MS SQLServer
    if (connection.indexOf("inetdae7") == -1) //$NON-NLS-1$
    {
        config.setProperty("hibernate.connection.url", connection); //$NON-NLS-1$
        config.setProperty("hibernate.dialect", dbConn.getDialect()); //$NON-NLS-1$
        config.setProperty("hibernate.connection.driver_class", driver); //$NON-NLS-1$

        // commenting out this line to avoid use of CGLIB
        // see http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html
        // search for hibernate.cglib.use_reflection_optimizer (which is the old name of the prop)
        //config.setProperty("hibernate.bytecode.use_reflection_optimizer", "true");
    } else {
        throw new RuntimeException("Connection string does not support SQLServer!"); //$NON-NLS-1$

        /*config.setProperty("hibernate.connection.url", connection + "?database="+ databaseName);
        config.setProperty("hibernate.dialect","org.hibernate.dialect.SQLServerDialect");
        config.setProperty("hibernate.connection.driver_class","com.inet.tds.TdsDriver");
        */
    }
    //else if(hostName.indexOf("sqlserver")!=-1){//jdbc:inetdae7:localhost?database=KS_fish
    //  config.setProperty("hibernate.connection.url",hostName + ";DatabaseName="+databaseName);
    //  config.setProperty("hibernate.dialect","net.sf.hibernate.dialect.SQLServerDialect");
    //  config.setProperty("hibernate.connection.driver_class","com.microsoft.jdbc.sqlserver.SQLServerDriver");
    //} 

    // added to ease the transition from Hibernate 3.1 to 3.2
    config.addSqlFunction("count", new ClassicCountFunction()); //$NON-NLS-1$
    config.addSqlFunction("avg", new ClassicAvgFunction()); //$NON-NLS-1$
    config.addSqlFunction("sum", new ClassicSumFunction()); //$NON-NLS-1$
}

From source file:magoffin.matt.ma2.dao.hbm.LocalSessionFactoryBean.java

License:Open Source License

@Override
protected void postProcessConfiguration(Configuration config) throws HibernateException {
    super.postProcessConfiguration(config);
    if (!CollectionUtils.isEmpty(sqlFunctions)) {
        for (Map.Entry<String, SQLFunction> me : sqlFunctions.entrySet()) {
            if (logger.isInfoEnabled()) {
                logger.info("Registering SQL function [" + me.getKey() + "] class ["
                        + me.getValue().getClass().getName() + "]");
            }/*from  ww w.  ja v  a  2s.  c o m*/
            config.addSqlFunction(me.getKey(), me.getValue());
        }
    }
}

From source file:nl.strohalm.cyclos.spring.CustomSessionFactoryBean.java

License:Open Source License

@Override
protected void postProcessConfiguration(final Configuration config) throws HibernateException {
    // Set classic functions to return, ie, Integer on count, not Long
    // New to Hibernate 3.2, and would affect a large number of classes
    config.addSqlFunction("count", new ClassicCountFunction());
    config.addSqlFunction("avg", new ClassicAvgFunction());
    config.addSqlFunction("sum", new ClassicSumFunction());
}

From source file:org.sakaiproject.springframework.orm.hibernate.AddableSessionFactoryBean.java

License:Educational Community License

/**
 * Provide backwards compatibility with Hibernate 3.1.x behavior for
 * aggregate functions.//  w w  w . j  a  va 2s .c  om
 */
@Override
protected Configuration newConfiguration() throws HibernateException {
    final Configuration classicCfg = new Configuration();
    classicCfg.addSqlFunction("count", new ClassicCountFunction());
    classicCfg.addSqlFunction("avg", new ClassicAvgFunction());
    classicCfg.addSqlFunction("sum", new ClassicSumFunction());
    return classicCfg;
}

From source file:org.squashtest.tm.domain.jpql.SessionFactoryEnhancer.java

License:Open Source License

public static void registerExtensions(Configuration hibConfig, FnSupport... fnSupports) {

    // aggregate function wrappers
    hibConfig.addSqlFunction(FN_NAME_SUM, new StandardSQLFunction("sum"));
    hibConfig.addSqlFunction(FN_NAME_MIN, new StandardSQLFunction("min"));
    hibConfig.addSqlFunction(FN_NAME_MAX, new StandardSQLFunction("max"));
    hibConfig.addSqlFunction(FN_NAME_AVG, new StandardSQLFunction("avg", DoubleType.INSTANCE));
    hibConfig.addSqlFunction(FN_NAME_CNT, new SCountDistinctFunction());

    // boolean case when

    // database-specific functions
    for (FnSupport support : fnSupports) {
        switch (support) {
        case GROUP_CONCAT:
            hibConfig.addSqlFunction(FN_NAME_GROUP_CONCAT,
                    new GroupConcatFunction(FN_NAME_GROUP_CONCAT, StringType.INSTANCE));
            break;
        case STR_AGG:
            hibConfig.addSqlFunction(FN_NAME_GROUP_CONCAT,
                    new StringAggFunction(FN_NAME_GROUP_CONCAT, StringType.INSTANCE));
            break;
        case EXTRACT_WEEK:
            hibConfig.addSqlFunction(FN_NAME_WEEK, new ExtractWeek(FN_NAME_WEEK, IntegerType.INSTANCE));
        }/*w w  w .java2 s. co m*/
    }

}