Search in sources :

Example 1 with CriteriaImpl

use of org.hibernate.internal.CriteriaImpl in project midpoint by Evolveum.

the class HibernateToSqlTranslator method toSql.

/**
     * 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
     * @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);
    }
}
Also used : Field(java.lang.reflect.Field) CriteriaImpl(org.hibernate.internal.CriteriaImpl) SystemException(com.evolveum.midpoint.util.exception.SystemException) CriteriaLoader(org.hibernate.loader.criteria.CriteriaLoader) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SessionImpl(org.hibernate.internal.SessionImpl) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Aggregations

SystemException (com.evolveum.midpoint.util.exception.SystemException)1 Field (java.lang.reflect.Field)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 CriteriaImpl (org.hibernate.internal.CriteriaImpl)1 SessionImpl (org.hibernate.internal.SessionImpl)1 CriteriaLoader (org.hibernate.loader.criteria.CriteriaLoader)1