hibernate将hql转换成count(*)的方法,支持所有的数据库oracle,mysql等

protected String prepareCountHql(String hql) {
//String sql = "select b.id, b.user_name, b.create_date from users b ";
//ParameterMetadata parameterMetadata = getQueryPlanCache().getSQLParameterMetadata(sql);

// NativeSQLQuerySpecification spec = new NativeSQLQuerySpecification();
// getQueryPlanCache().getNativeSQLQueryPlan(spec);

//SQLQuery sqlQuery = createSqlQuery(sql, User.class);
// System.out.println(Arrays.toString(sqlQuery.getReturnAliases()));
// System.out.println(Arrays.toString(sqlQuery.getReturnTypes()));
//Object objects = sqlQuery.list();

// getCountSql(hql, sessionFactory);
String fromHql = hql;
fromHql = "from" + StringUtils.substringAfter(fromHql, "from");
fromHql = StringUtils.substringBefore(fromHql, "order by");
int whereIndex = fromHql.indexOf("where");
int leftIndex = fromHql.indexOf("left join");
if (leftIndex >= 0) {
if (whereIndex >= 0) {
String temp = StringUtils.substringBefore(fromHql, "left");
fromHql = temp + " where " + StringUtils.substringAfter(fromHql, "where");
} else {
fromHql = StringUtils.substringBefore(fromHql, "left");
}
}
String countHql = "select count(*) " + fromHql;
return countHql;
}


protected String getCountSql(String originalHql, SessionFactory sessionFactory) {
//long d = System.nanoTime();
SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
HQLQueryPlan hqlQueryPlan = sessionFactoryImplementor.getQueryPlanCache().getHQLQueryPlan(originalHql, false, Collections.emptyMap());
String[] sqls = hqlQueryPlan.getSqlStrings();
//System.out.println(Arrays.toString(sqls));


// System.out.println(Arrays.toString(hqlQueryPlan.getReturnMetadata()
// .getReturnAliases()));
// System.out.println(Arrays.toString(hqlQueryPlan.getReturnMetadata()
// .getReturnTypes()));


// QueryTranslatorImpl queryTranslator = new
// QueryTranslatorImpl(originalHql, originalHql, Collections.emptyMap(),
// sessionFactoryImplementor);
//
// //org.hibernate.hql.internal.ast.QueryTranslatorImpl queryTranslator2
// = new org.hibernate.hql.internal.ast.QueryTranslatorImpl(originalHql,
// originalHql, Collections.emptyMap(), sessionFactoryImplementor);
//
// queryTranslator.compile(Collections.EMPTY_MAP, false);
// String countSql = "select count(*) from (" +
// queryTranslator.getSQLString() + ") tmp_count_t";
String countSql = "select count(*) from (" + sqls[0] + ") count";
//System.out.println(System.nanoTime() - d);
return countSql;
}

public SessionFactoryImplementor getSessionFactoryImplementor() {
return (SessionFactoryImplementor) getSessionFactory();
}

public QueryPlanCache getQueryPlanCache() {
return getSessionFactoryImplementor().getQueryPlanCache();
}

public HQLQueryPlan getHqlQueryPlan(String hql) {
return getQueryPlanCache().getHQLQueryPlan(hql, false, Collections.emptyMap());
}

protected String prepareCountSql(String sql) {
// String fromSql = sql;
// fromSql = "from" + StringUtils.substringAfter(fromSql, "from");
// fromSql = StringUtils.substringBefore(fromSql, "order by");
// String countSql = "select count(*) count " + fromSql;
// return countSql;
return getCountSql(sql, getSessionFactory());
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值