Skip to content

Commit

Permalink
Account for creation of subRow when field with relationship is not fi…
Browse files Browse the repository at this point in the history
…rst field and batch size is hit
  • Loading branch information
Sheikah45 committed Nov 20, 2024
1 parent 9f2c06d commit 9f3f4c6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,26 @@ protected Object processItem(ReportQuery query, AbstractRecord row, Vector toMan
AbstractRecord subRow = row;
// Check if at the start of the row, then avoid building a subRow.
if (itemIndex > 0) {
Vector<DatabaseField> trimedFields = new NonSynchronizedSubVector<>(row.getFields(), itemIndex, rowSize);
Vector trimedValues = new NonSynchronizedSubVector(row.getValues(), itemIndex, rowSize);
subRow = new DatabaseRecord(trimedFields, trimedValues);
BatchFetchPolicy batchFetchPolicy = query.getBatchFetchPolicy();
if (batchFetchPolicy != null && batchFetchPolicy.isIN()) {

List<AbstractRecord> subRows = new ArrayList<>(toManyData.size());
for (AbstractRecord parentRow : (Vector<AbstractRecord>) toManyData) {
Vector<DatabaseField> trimedParentFields = new NonSynchronizedSubVector<>(parentRow.getFields(), itemIndex, rowSize);
Vector trimedParentValues = new NonSynchronizedSubVector<>(parentRow.getValues(), itemIndex, rowSize);
subRows.add(new DatabaseRecord(trimedParentFields, trimedParentValues));
}

for (DatabaseMapping subMapping : descriptor.getMappings()) {
batchFetchPolicy.setDataResults(subMapping, subRows);
}

subRow = subRows.get(toManyData.indexOf(row));
} else {
Vector<DatabaseField> trimedFields = new NonSynchronizedSubVector<>(row.getFields(), itemIndex, rowSize);
Vector trimedValues = new NonSynchronizedSubVector(row.getValues(), itemIndex, rowSize);
subRow = new DatabaseRecord(trimedFields, trimedValues);
}
}
if (mapping != null && mapping.isAggregateObjectMapping()){
value = ((AggregateObjectMapping)mapping).buildAggregateFromRow(subRow, null, null, joinManager, query, false, query.getSession(), true);
Expand Down
14 changes: 14 additions & 0 deletions jpa/eclipselink.jpa.testapps/jpa.test.batchfetch/pom.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<!--
Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0,
or the Eclipse Distribution License v. 1.0 which is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.eclipse.persistence.jpa.testapps.batchfetch;

public record Count(long value, Employee employee) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import jakarta.persistence.TypedQuery;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.persistence.config.QueryHints;
import org.eclipse.persistence.jpa.testapps.batchfetch.BatchFetchTableCreator;
import org.eclipse.persistence.jpa.testapps.batchfetch.Company;
import org.eclipse.persistence.jpa.testapps.batchfetch.Count;
import org.eclipse.persistence.jpa.testapps.batchfetch.Employee;
import org.eclipse.persistence.jpa.testapps.batchfetch.Record;
import org.eclipse.persistence.testing.framework.jpa.junit.JUnitTestCase;
Expand All @@ -43,6 +45,7 @@ public static Test suite() {
suite.addTest(new BatchFetchJUnitTest("testSetup"));
suite.addTest(new BatchFetchJUnitTest("testSelectRoot"));
suite.addTest(new BatchFetchJUnitTest("testSelectNonRoot"));
suite.addTest(new BatchFetchJUnitTest("testSelectNonRootWithOffsetAndLimit"));
return suite;
}

Expand Down Expand Up @@ -122,6 +125,25 @@ public void testSelectNonRoot() {
}
}

public void testSelectNonRootWithOffsetAndLimit() {
EntityManager em = createEntityManager();
em.getEntityManagerFactory().getCache().evictAll();

try {
TypedQuery<Count> q = em.createQuery("SELECT new org.eclipse.persistence.jpa.testapps.batchfetch.Count(count(r.employee), r.employee) FROM Record r group by r.employee", Count.class);
q.setHint(QueryHints.BATCH_SIZE, 1);
List<Count> result = q.getResultList();
assertEquals("Not all rows are selected", 3, result.size());
List<Employee> employees = result.stream().map(Count::employee).filter(Objects::nonNull).toList();
assertEquals("Not all counts have employees", 3, result.size());
List<Company> companies = employees.stream().map(Employee::getCompany).filter(Objects::nonNull).toList();
assertEquals("Not all employees have companies", 3, companies.size());
} catch (RuntimeException e) {
closeEntityManager(em);
throw e;
}
}

@Override
public String getPersistenceUnitName() {
return "batchfetch";
Expand Down

0 comments on commit 9f3f4c6

Please sign in to comment.