Example usage for org.hibernate.engine.spi SessionFactoryImplementor getSqlFunctionRegistry

List of usage examples for org.hibernate.engine.spi SessionFactoryImplementor getSqlFunctionRegistry

Introduction

In this page you can find the example usage for org.hibernate.engine.spi SessionFactoryImplementor getSqlFunctionRegistry.

Prototype

SQLFunctionRegistry getSqlFunctionRegistry();

Source Link

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.function.AbstractHibernateEntityManagerFactoryIntegrator.java

License:Apache License

@SuppressWarnings("unchecked")
private Map<String, SQLFunction> getFunctions(Session s) {
    if (MAJOR < 5 || (MAJOR == 5 && MINOR == 0 && FIX == 0 && "Beta1".equals(TYPE))) {
        // Implementation detail: Hibernate uses a mutable map, so we can do this
        return getDialect(s).getFunctions();
    } else {//from   w  w w . j a v  a  2  s.co  m
        SessionFactoryImplementor sf = (SessionFactoryImplementor) s.getSessionFactory();
        SQLFunctionRegistry registry = sf.getSqlFunctionRegistry();
        Exception ex;

        // We have to retrieve the functionMap the old fashioned way via reflection :(
        Field f = null;
        boolean madeAccessible = false;

        try {
            f = SQLFunctionRegistry.class.getDeclaredField("functionMap");
            madeAccessible = !f.isAccessible();

            if (madeAccessible) {
                f.setAccessible(true);
            }

            return (Map<String, SQLFunction>) f.get(registry);
        } catch (NoSuchFieldException e) {
            ex = e;
        } catch (IllegalArgumentException e) {
            // This can never happen
            ex = e;
        } catch (IllegalAccessException e) {
            ex = e;
        } finally {
            if (f != null && madeAccessible) {
                f.setAccessible(false);
            }
        }

        throw new RuntimeException(
                "Could not access the function map to dynamically register functions. Please report this version of hibernate("
                        + VERSION_STRING + ") so we can provide support for it!",
                ex);
    }
}

From source file:com.blazebit.persistence.integration.hibernate.base.function.AbstractHibernateEntityManagerFactoryIntegrator.java

License:Apache License

private void replaceFunctions(Session s, Map<String, SQLFunction> newFunctions) {
    if (MAJOR < 5 || (MAJOR == 5 && MINOR == 0 && FIX == 0 && "Beta1".equals(TYPE))) {
        Exception ex;/*w  w w. ja v a2 s  .c  o  m*/
        Field f = null;
        boolean madeAccessible = false;

        try {
            f = Dialect.class.getDeclaredField("sqlFunctions");
            madeAccessible = !f.isAccessible();

            if (madeAccessible) {
                f.setAccessible(true);
            }

            f.set(getDialect(s), newFunctions);
            return;
        } catch (NoSuchFieldException e) {
            ex = e;
        } catch (IllegalArgumentException e) {
            // This can never happen
            ex = e;
        } catch (IllegalAccessException e) {
            ex = e;
        } finally {
            if (f != null && madeAccessible) {
                f.setAccessible(false);
            }
        }
        throw new RuntimeException(
                "Could not access the function map to dynamically register functions. Please report this version of hibernate("
                        + VERSION_STRING + ") so we can provide support for it!",
                ex);
    } else {
        SessionFactoryImplementor sf = (SessionFactoryImplementor) s.getSessionFactory();
        SQLFunctionRegistry registry = sf.getSqlFunctionRegistry();
        Exception ex;

        // We have to retrieve the functionMap the old fashioned way via reflection :(
        Field f = null;
        boolean madeAccessible = false;

        try {
            f = SQLFunctionRegistry.class.getDeclaredField("functionMap");
            madeAccessible = !f.isAccessible();

            if (madeAccessible) {
                f.setAccessible(true);
            }

            f.set(registry, newFunctions);
            return;
        } catch (NoSuchFieldException e) {
            ex = e;
        } catch (IllegalArgumentException e) {
            // This can never happen
            ex = e;
        } catch (IllegalAccessException e) {
            ex = e;
        } finally {
            if (f != null && madeAccessible) {
                f.setAccessible(false);
            }
        }

        throw new RuntimeException(
                "Could not access the function map to dynamically register functions. Please report this version of hibernate("
                        + VERSION_STRING + ") so we can provide support for it!",
                ex);
    }
}