减少hibernate中的准备语句计数

o2rvlv0m  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(346)

我正在努力与hibernate准备语句计数。我使用以下jpa条件查询:

int count = 30
EntityManager manager = ...
CriteriaBuilder builder = manager.getCriteriaBuilder();

CriteriaQuery<String> select = builder.createQuery(String.class);
Root<AdministrationParameter> root = select.from(AdministrationParameter.class);
select.select(root.get(AdministrationParameter_.value));

ParameterExpression<String> peF1 = builder.parameter(AdministrationParameter_.context.getBindableJavaType(), "f1");
ParameterExpression<String> peF2 = builder.parameter(AdministrationParameter_.parameter.getBindableJavaType(), "f2");

Predicate p1 = builder.equal(root.get(AdministrationParameter_.context), peF1);
Predicate p2 = builder.equal(root.get(AdministrationParameter_.parameter), peF2);

select.where(p1, p2);

List<String> results = Collections.emptyList();
TypedQuery<String> query = manager.createQuery(select);

for (int i = 0; i < count; i++) {
    query.setParameter(peF1, administrationParameterTypeInterface.getContext());
    query.setParameter(peF2, administrationParameterTypeInterface.getParameter());
    query.getResultList();
}

这个 count 变量是执行查询n次,例如在后台运行db跟踪(查询是针对db2数据库执行的)。
假设 count = 30 db2跟踪说,有“30 prepares”和“30 descriptions”,即“statement found count=30”。
hibernate给了我相同的值:

EntityManagerFactory factory = ...;
SessionFactory sessionFactory = factory.unwrap(SessionFactory.class);
statistics = sessionFactory.getStatistics();
Statistics statistics = statistics.setStatisticsEnabled(true);

...running the query above...

System.out.println("prepared statement count: " + statistics.getPrepareStatementCount());//is 30
System.out.println("query cache hit count: " + statistics.getQueryCacheHitCount());//0
System.out.println("query cache miss count: " + statistics.getQueryCacheMissCount());//0
System.out.println("query execution count: " + statistics.getQueryExecutionCount());//30

根据javadochttps://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/stat/statistics.html 这个 statistics.getPrepareStatementCount() 是“获得的准备好的语句数”。
不是应该是1吗?

woobm2wo

woobm2wo1#

您使用的是哪个hibernate版本?这可能是新版本中已修复的错误。如果更新没有帮助,请在问题跟踪器中创建问题(https://hibernate.atlassian.net)使用测试用例(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/jpaunittestcase.java)这再现了这个问题。

相关问题