Skip to content

Commit

Permalink
Fix for ReportQuery not fetching relationships with BatchFetch.IN (ec…
Browse files Browse the repository at this point in the history
…lipse-ee4j#2311)


Co-authored-by: Radek Felcman <[email protected]>
  • Loading branch information
Sheikah45 and rfelcman authored Nov 25, 2024
1 parent 954fef6 commit 6736e5c
Show file tree
Hide file tree
Showing 13 changed files with 610 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,12 @@ public Object executeDatabaseQuery() throws DatabaseException {
return getDescriptor().getInterfacePolicy().selectAllObjectsUsingMultipleTableSubclassRead(this);
}

return buildObjects(getQueryMechanism().selectAllReportQueryRows());
List<AbstractRecord> rows = getQueryMechanism().selectAllReportQueryRows();
if ((this.batchFetchPolicy != null) && this.batchFetchPolicy.isIN()) {
this.batchFetchPolicy.setDataResults(rows);
}

return buildObjects((Vector) rows);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 1998, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 IBM Corporation. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024 IBM Corporation. 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
Expand Down Expand Up @@ -39,6 +39,7 @@
import org.eclipse.persistence.internal.expressions.FunctionExpression;
import org.eclipse.persistence.internal.expressions.MapEntryExpression;
import org.eclipse.persistence.internal.helper.ConversionManager;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.helper.NonSynchronizedSubVector;
import org.eclipse.persistence.internal.queries.JoinedAttributeManager;
import org.eclipse.persistence.internal.queries.ReportItem;
Expand Down Expand Up @@ -250,9 +251,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 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 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 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
19 changes: 18 additions & 1 deletion jpa/eclipselink.jpa.test/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2019, 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
Expand Down Expand Up @@ -364,6 +364,7 @@
<item>eclipselink-remote</item>
<item>jpa-performance</item>
<item>jpa-performance2</item>
<item>eclipselink-batchfetch-model</item>
<item>eclipselink-beanvalidation-model</item>
<item>eclipselink-beanvalidation-dynamic-model</item>
<item>eclipselink-identity-model</item>
Expand Down Expand Up @@ -504,6 +505,7 @@
<item>eclipselink-metamodel-model</item>
<item>eclipselink-delimited-model</item>
<item>eclipselink-extensibility-model</item>
<item>eclipselink-batchfetch-model</item>
<item>eclipselink-beanvalidation-model</item>
<item>eclipselink-beanvalidation-dynamic-model</item>
<item>eclipselink-xml-mapping-metadata-complete</item>
Expand Down Expand Up @@ -1058,6 +1060,7 @@
<additionalClasspathElement>${project.build.directory}/eclipselink-ddl-generation-model.jar</additionalClasspathElement>
<additionalClasspathElement>${project.build.directory}/eclipselink-metamodel-model.jar</additionalClasspathElement>
<additionalClasspathElement>${project.build.directory}/eclipselink-delimited-model.jar</additionalClasspathElement>
<additionalClasspathElement>${project.build.directory}/eclipselink-batchfetch-model.jar</additionalClasspathElement>
<additionalClasspathElement>${project.build.directory}/eclipselink-cascade-deletes-model.jar</additionalClasspathElement>
<additionalClasspathElement>${project.build.directory}/eclipselink-xml-mapping-metadata-complete.jar</additionalClasspathElement>
</additionalClasspathElements>
Expand Down Expand Up @@ -1651,6 +1654,20 @@
<test-client-class>org.eclipse.persistence.testing.tests.jpa22.advanced.JPA22TestSuite</test-client-class>
</properties>
</profile>
<profile>
<id>server-test-jpa-batchfetch</id>
<properties>
<skip-test-jpa-srg>true</skip-test-jpa-srg>
<skip-server-test-jpa>false</skip-server-test-jpa>
<skip-jpa-jse-build>true</skip-jpa-jse-build>
<puName>batchfetch</puName>
<!--modelname property name is used there due substitution string in application.xml-->
<modelname>eclipselink-batchfetch-model</modelname>
<extra-model>eclipselink-batchfetch-model</extra-model>
<packageName>batchfetch</packageName>
<test-client-class>org.eclipse.persistence.testing.tests.jpa.batchfetch.BatchFetchJUnitTest</test-client-class>
</properties>
</profile>
<profile>
<id>server-test-jpa-beanvalidation</id>
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
-->

<component xmlns="http://maven.apache.org/ASSEMBLY-COMPONENT/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY-COMPONENT/2.0.0 http://maven.apache.org/xsd/assembly-component-2.0.0.xsd">
<fileSets>
<fileSet>
<directory>${project.build.testOutputDirectory}</directory>
<outputDirectory>.</outputDirectory>
<includes>
<include>org/eclipse/persistence/testing/models/jpa/batchfetch/**</include>
</includes>
</fileSet>
</fileSets>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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
*/

// Contributors:

package org.eclipse.persistence.testing.models.jpa.batchfetch;

import org.eclipse.persistence.tools.schemaframework.FieldDefinition;
import org.eclipse.persistence.tools.schemaframework.TableCreator;
import org.eclipse.persistence.tools.schemaframework.TableDefinition;

public class BatchFetchTableCreator extends TableCreator {
public BatchFetchTableCreator() {
setName("BatchFetchProject");

addTableDefinition(buildCompanyTable());
addTableDefinition(buildEmployeeTable());
addTableDefinition(buildRecordTable());
}

public TableDefinition buildRecordTable() {
TableDefinition table = new TableDefinition();
table.setName("BATCH_IN_RECORD");

FieldDefinition fieldID = new FieldDefinition();
fieldID.setName("ID");
fieldID.setTypeName("NUMBER");
fieldID.setSize(19);
fieldID.setSubSize(0);
fieldID.setIsPrimaryKey(true);
fieldID.setIsIdentity(true);
fieldID.setShouldAllowNull(false);
table.addField(fieldID);

FieldDefinition fieldUSER = new FieldDefinition();
fieldUSER.setName("EMPLOYEE_ID");
fieldUSER.setTypeName("NUMBER");
fieldUSER.setSize(19);
fieldUSER.setSubSize(0);
fieldUSER.setIsPrimaryKey(false);
fieldUSER.setIsIdentity(false);
fieldUSER.setShouldAllowNull(false);
fieldUSER.setForeignKeyFieldName("BATCH_IN_EMPLOYEE.ID");
table.addField(fieldUSER);

return table;
}

public TableDefinition buildCompanyTable() {
TableDefinition table = new TableDefinition();
table.setName("BATCH_IN_COMPANY");

FieldDefinition fieldID = new FieldDefinition();
fieldID.setName("ID");
fieldID.setTypeName("NUMBER");
fieldID.setSize(19);
fieldID.setSubSize(0);
fieldID.setIsPrimaryKey(true);
fieldID.setIsIdentity(true);
fieldID.setShouldAllowNull(false);
table.addField(fieldID);

return table;
}


public TableDefinition buildEmployeeTable() {
TableDefinition table = new TableDefinition();
table.setName("BATCH_IN_EMPLOYEE");

FieldDefinition fieldID = new FieldDefinition();
fieldID.setName("ID");
fieldID.setTypeName("NUMBER");
fieldID.setSize(19);
fieldID.setSubSize(0);
fieldID.setIsPrimaryKey(true);
fieldID.setIsIdentity(true);
fieldID.setShouldAllowNull(false);
table.addField(fieldID);

FieldDefinition fieldCompany = new FieldDefinition();
fieldCompany.setName("COMPANY_ID");
fieldCompany.setTypeName("NUMBER");
fieldCompany.setSize(19);
fieldCompany.setSubSize(0);
fieldCompany.setIsPrimaryKey(false);
fieldCompany.setIsIdentity(false);
fieldCompany.setShouldAllowNull(false);
fieldCompany.setForeignKeyFieldName("BATCH_IN_COMPANY.ID");
table.addField(fieldCompany);

return table;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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
*/

// Contributors:

package org.eclipse.persistence.testing.models.jpa.batchfetch;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.eclipse.persistence.annotations.BatchFetch;
import org.eclipse.persistence.annotations.BatchFetchType;

import java.util.List;

@Entity
@Table(name = "BATCH_IN_COMPANY")
public class Company {
@Id
private long id;

public Company() {
}

public Company(long id) {
this.id = id;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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
*/

// Contributors:

package org.eclipse.persistence.testing.models.jpa.batchfetch;

public class Count {

private long value;
private Employee emp;

public Count(long value, Employee emp) {
this.value = value;
this.emp = emp;
}

public long getValue() {
return value;
}

public void setValue(long value) {
this.value = value;
}

public Employee getEmp() {
return emp;
}

public void setEmp(Employee emp) {
this.emp = emp;
}
}
Loading

0 comments on commit 6736e5c

Please sign in to comment.