您的位置:首页 > 博客中心 > 数据库 >

hql 转化为 sql ,满足仅仅用hibernate无法实现的sql构建

时间:2022-03-14 00:55

如下方法能将hql转换为sql,便于在系统总体是用hibernate查询的情况下,加入一些特殊查询条件的sql语句,满足仅仅用hibernate无法实现的sql构建。 

protected String hqlToSql(String hql,
   org.hibernate.SessionFactory sessionFactory) throws Exception {
  org.hibernate.hql.ast.QueryTranslatorImpl queryTranslator = new org.hibernate.hql.ast.QueryTranslatorImpl(
    hql, hql, java.util.Collections.EMPTY_MAP,
    (org.hibernate.engine.SessionFactoryImplementor) sessionFactory);

  queryTranslator.compile(java.util.Collections.EMPTY_MAP, false);

  return queryTranslator.getSQLString();
 }


热门排行

今日推荐

热门手游