Example usage for org.hibernate.loader.criteria CriteriaLoader CriteriaLoader

List of usage examples for org.hibernate.loader.criteria CriteriaLoader CriteriaLoader

Introduction

In this page you can find the example usage for org.hibernate.loader.criteria CriteriaLoader CriteriaLoader.

Prototype

public CriteriaLoader(final OuterJoinLoadable persister, final SessionFactoryImplementor factory,
            final CriteriaImpl criteria, final String rootEntityName,
            final LoadQueryInfluencers loadQueryInfluencers) throws HibernateException 

Source Link

Usage

From source file:com.evolveum.midpoint.repo.sql.util.HibernateToSqlTranslator.java

License:Apache License

/**
 * Do not use in production code! Only for testing purposes only. Used for example during query engine upgrade.
 * Method provides translation from hibernate {@link Criteria} to plain SQL string query.
 *
 * @param criteria/*  ww w.  j  av a2  s  .c  om*/
 * @return SQL string, null if criteria parameter was null.
 */
public static String toSql(Criteria criteria) {
    if (criteria == null) {
        return null;
    }

    try {
        CriteriaImpl c;
        if (criteria instanceof CriteriaImpl) {
            c = (CriteriaImpl) criteria;
        } else {
            CriteriaImpl.Subcriteria subcriteria = (CriteriaImpl.Subcriteria) criteria;
            c = (CriteriaImpl) subcriteria.getParent();
        }
        SessionImpl s = (SessionImpl) c.getSession();
        SessionFactoryImplementor factory = s.getSessionFactory();
        String[] implementors = factory.getImplementors(c.getEntityOrClassName());
        CriteriaLoader loader = new CriteriaLoader(
                (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), factory, c, implementors[0],
                s.getLoadQueryInfluencers());
        Field f = OuterJoinLoader.class.getDeclaredField("sql");
        f.setAccessible(true);
        return (String) f.get(loader);
    } catch (Exception ex) {
        throw new SystemException(ex.getMessage(), ex);
    }
}

From source file:id.co.sambaltomat.core.dao.hibernate.GenericDaoHibernate.java

private String criteriaToString(Criteria criteria) {
    String sql = null;/*from w  w  w.  ja  v  a 2 s  . c o m*/

    try {
        CriteriaImpl c = (CriteriaImpl) criteria;
        SessionImpl s = (SessionImpl) c.getSession();
        SessionFactoryImplementor factory = (SessionFactoryImplementor) s.getSessionFactory();
        String[] implementors = factory.getImplementors(c.getEntityOrClassName());
        CriteriaLoader loader = new CriteriaLoader(
                (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), factory, c, implementors[0],
                s.getEnabledFilters());
        Field f = OuterJoinLoader.class.getDeclaredField("sql");
        f.setAccessible(true);
        sql = (String) f.get(loader);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return sql;
}

From source file:org.geolatte.common.cql.hibernate.HibernateHqlAndCriteriaToSqlTranslator.java

License:Open Source License

public String toSql(Criteria criteria) {
    try {/*from ww  w .  j a  va 2  s. co  m*/
        CriteriaImpl c = (CriteriaImpl) criteria;
        SessionImpl s = (SessionImpl) c.getSession();
        SessionFactoryImplementor factory = (SessionFactoryImplementor) s.getSessionFactory();
        String[] implementors = factory.getImplementors(c.getEntityOrClassName());
        CriteriaLoader loader = new CriteriaLoader(
                (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), factory, c, implementors[0],
                s.getLoadQueryInfluencers());
        Field f = OuterJoinLoader.class.getDeclaredField("sql");
        f.setAccessible(true);
        return (String) f.get(loader);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}