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

2014-11-24 08:42:01 · 作者: · 浏览: 0
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, getSession