Hibernate converts hql to count (*) and supports all databases such as oracle and mysql.

Source: Internet
Author: User

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());}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.