eclipselink嵌套批量提取

时间:2014-09-08 12:24:38

标签: jpa eclipselink batch-fetching

我已经建立了一个包含3个实体的非常简单的模型:

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

我想从抓取Employee批量抓取AddressDepartment,所以我有以下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注释,那么查询就可以完美运行。

如何在不使用注释的情况下解决此问题(因为注释会强制对我的所有查询进行批量查询)?

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

2 个答案:

答案 0 :(得分: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的批量提取。

答案 1 :(得分: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必须使用新的映射Vector调用descriptor.setMappings。