广州疯狂软件学院拥有三大课程体系包括:java,android,ios课程,更多java,android,ios技术知识,疯狂软件官网与你分享。
今天随中国IT实验室小编一起体验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());
}
疯狂Java培训专注软件开发培训,提升学员就业能力,重点提升实践动手能力。高薪从IT名企请来项目经理为学员亲自授课,对学员进行实战教学,在没有工作经验的学员,在疯狂java,通过大量全真经典企业级项目进行集中培训,学员通过数月培训都可获得1-2年的工作经验,进而在同类的求职者中脱颖而出。疯狂Java培训让你体会java编程的快乐,项目开发的兴奋,成就感,通过短短几个月的时间,让你在半年的时间内掌握8-10万的代码量,掌握Java核心技术,迅速成为技能型的现代化高端人才,迅速获得高薪就业!