我建立了一个非常简单的模型,其中包含 3 个实体:
Department
其中有多个(关系employees
)Employee
其中有多个(relaionshipaddresses
)Address
我想批量获取Employee
和Address
从获取中获取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
,那么查询就完美了。
我怎样才能在不使用注释的情况下解决这个问题(因为注释会强制对我的所有查询进行批量查询)?
如果您需要更多信息或完整代码,我很乐意提供。