gpt4 book ai didi

jpa-2.0 - EclipseLink JPA : Can I run multiple queries from one builder?

转载 作者:行者123 更新时间:2023-12-01 02:44:23 25 4
gpt4 key购买 nike

我有一种方法可以构建和运行 Criteria 查询。该查询执行我想要的操作,特别是它根据用户输入过滤(和排序)记录。

此外,查询大小受限于屏幕上的记录数。这很重要,因为数据表可能非常大。

但是,如果应用过滤器,我想计算如果查询不受限制将返回的记录数。所以这意味着运行两个查询:一个来获取记录,然后一个来计算整个集合中的记录。它看起来像这样:

public List<Log> runQuery(TableQueryParameters tqp) {

// get the builder, query, and root

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Log> query = builder.createQuery(Log.class);
Root<Log> root = query.from(Log.class);

// build the requested filters

Predicate filter = null;
for (TableQueryParameters.FilterTerm ft : tqp.getFilterTerms()) {

// this section runs trough the user input and constructs the
// predicate

}
if (filter != null) query.where(filter);

// attach the requested ordering

List<Order> orders = new ArrayList<Order>();
for (TableQueryParameters.SortTerm st : tqp.getActiveSortTerms()) {

// this section constructs the Order objects

}
if (!orders.isEmpty()) query.orderBy(orders);

// run the query

TypedQuery<Log> typedQuery = em.createQuery(query);
typedQuery.setFirstResult((int) tqp.getStartRecord());
typedQuery.setMaxResults(tqp.getPageSize());
List<Log> list = typedQuery.getResultList();

// if we need the result size, fetch it now

if (tqp.isNeedResultSize()) {
CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
countQuery.select(builder.count(countQuery.from(Log.class)));
if (filter != null) countQuery.where(filter);
tqp.setResultSize(em.createQuery(countQuery).getSingleResult().intValue());
}

return list;
}

因此,我在同一个 CriteriaBuilder 上调用了两次 createQuery 并在它们之间共享 Predicate 对象(过滤器)。当我运行第二个查询时,有时会收到以下消息:

Exception [EclipseLink-6089] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.QueryException Exception Description: The expression has not been initialized correctly. Only a single ExpressionBuilder should be used for a query. For parallel expressions, the query class must be provided to the ExpressionBuilder constructor, and the query's ExpressionBuilder must always be on the left side of the expression. Expression: [ Base com.myqwip.database.Log] Query: ReportQuery(referenceClass=Log ) at org.eclipse.persistence.exceptions.QueryException.noExpressionBuilderFound(QueryException.java:874) at org.eclipse.persistence.expressions.ExpressionBuilder.getDescriptor(ExpressionBuilder.java:195) at org.eclipse.persistence.internal.expressions.DataExpression.getMapping(DataExpression.java:214)



有人能告诉我为什么这个错误会间歇性地出现,我应该怎么做才能解决这个问题?

最佳答案

问题的简短回答:是的,您可以,但只能按顺序进行。

在上面的方法中,您开始创建第一个查询,然后开始创建第二个,执行第二个,然后执行第一个。

我有同样的问题。我不知道为什么它是间歇性的艰难。

换句话说,您开始创建第一个查询,在完成之前,您开始创建和执行另一个查询。

Hibernate 没有提示,但 eclipselink 不喜欢它。

如果您只是从查询计数开始,执行它,然后创建并执行另一个查询(您通过将其拆分为 2 种方法所做的),eclipselink 不会提示。

https://issues.jboss.org/browse/SEAMSECURITY-91

关于jpa-2.0 - EclipseLink JPA : Can I run multiple queries from one builder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7290678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com