~/home of geeks

Hibernate Criteria to SQL Translation

· 171 Wörter · 1 Minute(n) Lesedauer

hall full of ice figures, sci-fi

Some time ago I posted a helper-class to transform HQL to SQL for debugging purposes. Meanwhile I found out how also to transform Criterias to SQL.

Please be aware that the solution below seems like a big dirty hack to me.

import java.lang.reflect.Field;
import java.util.Collections;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.hql.QueryTranslator;
import org.hibernate.hql.QueryTranslatorFactory;
import org.hibernate.hql.ast.ASTQueryTranslatorFactory;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.impl.SessionImpl;
import org.hibernate.loader.OuterJoinLoader;
import org.hibernate.loader.criteria.CriteriaLoader;
import org.hibernate.persister.entity.OuterJoinLoadable;
 
public class HibernateHqlAndCriteriaToSqlTranslator {
  private SessionFactory sessionFactory;
 
  public void setSessionFactory(SessionFactory sessionFactory){
    this.sessionFactory = sessionFactory;
  }
 
  public String toSql(Criteria criteria){
    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);
      return (String) f.get(loader);
    }
    catch(Exception e){
      throw new RuntimeException(e); 
    }
  }

  public String toSql(String hqlQueryText){
    if (hqlQueryText!=null && hqlQueryText.trim().length()>0){
      final QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
      final SessionFactoryImplementor factory = 
        (SessionFactoryImplementor) sessionFactory;
      final QueryTranslator translator = translatorFactory.
        createQueryTranslator(
          hqlQueryText, 
          hqlQueryText, 
          Collections.EMPTY_MAP, factory
        );
      translator.compile(Collections.EMPTY_MAP, false);
      return translator.getSQLString(); 
    }
    return null;
  }
}