3

我建立了一个非常简单的模型,其中包含 3 个实体:

  • Department其中有多个(关系employees
  • Employee其中有多个(relaionship addresses
  • Address

我想批量获取EmployeeAddress从获取中获取Department,所以我有以下 JPL:

select d from Department d

我添加了以下提示:

query.setHint(QueryHints.BATCH, "d.employees.addresses");

但是我经常收到以下错误:

Exception [EclipseLink-6089] (Eclipse Persistence Services - 2.4.2.v20130514-5956486): 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 my.jpa.test.Department]
Query: ReadAllQuery(referenceClass=Department sql="SELECT ID, NAME FROM DEPARTMENT")
    at org.eclipse.persistence.exceptions.QueryException.noExpressionBuilderFound(QueryException.java:904)
    at org.eclipse.persistence.expressions.ExpressionBuilder.getDescriptor(ExpressionBuilder.java:195)
    at org.eclipse.persistence.internal.expressions.DataExpression.getContainingDescriptor(DataExpression.java:214)
    at org.eclipse.persistence.internal.expressions.DataExpression.getMapping(DataExpression.java:221)
    at org.eclipse.persistence.internal.expressions.QueryKeyExpression.getMapping(QueryKeyExpression.java:426)
    at org.eclipse.persistence.mappings.DatabaseMapping.extractNestedExpressions(DatabaseMapping.java:464)
    at org.eclipse.persistence.mappings.ForeignReferenceMapping.prepareNestedBatchQuery(ForeignReferenceMapping.java:882)
    at org.eclipse.persistence.mappings.ForeignReferenceMapping.batchedValueFromRow(ForeignReferenceMapping.java:220)
    at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRow(ForeignReferenceMapping.java:2046)
    at org.eclipse.persistence.mappings.ForeignReferenceMapping.buildCloneFromRow(ForeignReferenceMapping.java:289)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoWorkingCopyClone(ObjectBuilder.java:1617)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneFromRow(ObjectBuilder.java:1769)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:672)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:609)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:564)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:777)
    at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:797)
    at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:434)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1150)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1109)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:393)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1197)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2879)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1607)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1589)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1554)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:231)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:411)
    at my.jpa.test.Department.main(Department.java:124)

如果我直接在关系上使用@BatchFetch注释@OneToMany,那么查询就完美了。

我怎样才能在不使用注释的情况下解决这个问题(因为注释会强制对我的所有查询进行批量查询)?

如果您需要更多信息或完整代码,我很乐意提供。

4

2 回答 2

1

我最近遇到了同样的问题,不幸的是我也无法解决。作为一种解决方法,我在父实体中使用 @NamedQuery 注释定义了 SELECT:

@Entity
@NamedQueries
(
    { 
        @NamedQuery(name = "Department.batchFetch", query = "Select d from Department d", 
        hints = 
        {
            (...)
            // works with JOIN/EXISTS too
            @QueryHint(name = QueryHints.BATCH_TYPE, value = "IN"),
            @QueryHint(name = QueryHints.BATCH, value = "d.employees.addresses")

        }), 
    }
)
public class Person 
{
    (...)
}

调用 NamedQuery 的方法将具有:

Query query = em.createNamedQuery(queryName); // "Department.batchFetch"
List<Department> result = query.getResultList();

这不完全是我想要的(我无法自定义我想在运行时加载的实体),但它胜过让所有查询对我使用带有 @BatchQuery 的批处理获取。

于 2014-09-30T08:59:18.570 回答
0

我通过使用定制器并创建一个扩展 OneToOneMapping 的类(但我认为它也适用于 ManyToOneMapping),通过覆盖 prepareNestedBatchQuery 方法解决了这个问题。

代替:

            if (expressionBuilder.getQueryClass() == null) {
                expressionBuilder.setSession(query.getSession().getRootSession(null));
                expressionBuilder.setQueryClass(query.getReferenceClass());
            }

和:

            if (expressionBuilder.getQueryClass() == null) {
                expressionBuilder.setSession(query.getSession().getRootSession(null));
                expressionBuilder.setQueryClass(query.getReferenceClass());
            } else if (expressionBuilder.getSession() == null) {
                expressionBuilder.setSession(query.getSession().getRootSession(null));
            }

您的 DescriptorCustomizer 必须使用新的映射向量调用 descriptor.setMappings。

于 2018-05-23T09:55:53.187 回答