hibernate criteria 转成 hql

hibernate的查询器Criteria可以生成hql, 注意要收集参数,查询时用到:

 public String getCriteriaSql(Criteria criteria) {
  CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;//转型  
  SessionImplementor session = criteriaImpl.getSession();//获取SESSION  
  SessionFactoryImplementor factory = session.getFactory();//获取FACTORY  
  CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS); 
  String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName()); 
  CriteriaJoinWalker walker = new CriteriaJoinWalker((OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), session.getEnabledFilters()); 
  String hql =  walker.getSQLString();
  hql = hql.replaceAll("this_", "e");
  return hql;
 }

你可能感兴趣的:(Hibernate)