Skip to content

Commit

Permalink
Fix for ReportQuery not fetching relationships with BatchFetch.IN (#2316
Browse files Browse the repository at this point in the history
)


Co-authored-by: Radek Felcman <[email protected]>
  • Loading branch information
Sheikah45 and rfelcman authored Nov 27, 2024
1 parent fc24a51 commit 2db4798
Show file tree
Hide file tree
Showing 13 changed files with 598 additions and 5 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
Expand Up @@ -239,9 +239,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
1 change: 1 addition & 0 deletions jpa/eclipselink.jpa.test/antbuild.properties
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ jpa.performance=jpa-performance
jpa.performance2=jpa-performance2
eclipselink.delimited.model=eclipselink-delimited-model
eclipselink.extensibility.model=eclipselink-extensibility-model
eclipselink.batchfetch.model=eclipselink-batchfetch-model
eclipselink.beanvalidation.model=eclipselink-beanvalidation-model
eclipselink.beanvalidation.dynamic.model=eclipselink-beanvalidation-dynamic-model

Expand Down
44 changes: 43 additions & 1 deletion jpa/eclipselink.jpa.test/antbuild.xml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@
<fileset file="${jpatest.basedir}/${eclipselink.cascade.deletes}.jar"/>
<fileset file="${jpatest.basedir}/${eclipselink.delimited.model}.jar"/>
<fileset file="${jpatest.basedir}/${eclipselink.extensibility.model}.jar"/>
<fileset file="${jpatest.basedir}/${eclipselink.batchfetch.model}.jar"/>
<fileset file="${jpatest.basedir}/${eclipselink.beanvalidation.model}.jar"/>
<fileset file="${jpatest.basedir}/${eclipselink.beanvalidation.dynamic.model}.jar"/>
<fileset file="${jpatest.basedir}/${eclipselink.composite.advanced.model}.jar"/>
Expand Down Expand Up @@ -656,6 +657,7 @@
<mkdir dir="${jpatest.basedir}/${build.dir}/${eclipselink.metamodel.model}"/>
<mkdir dir="${jpatest.basedir}/${build.dir}/${eclipselink.delimited.model}"/>
<mkdir dir="${jpatest.basedir}/${build.dir}/${eclipselink.extensibility.model}"/>
<mkdir dir="${jpatest.basedir}/${build.dir}/${eclipselink.batchfetch.model}"/>
<mkdir dir="${jpatest.basedir}/${build.dir}/${eclipselink.beanvalidation.model}"/>
<mkdir dir="${jpatest.basedir}/${build.dir}/${eclipselink.beanvalidation.dynamic.model}"/>
<mkdir dir="${jpatest.basedir}/${build.dir}/${eclipselink.composite.advanced.model}"/>
Expand Down Expand Up @@ -935,7 +937,7 @@
<target name="package" depends="config-classpath, package-annotation, package-ddl, package-xml-only, package-xml-merge, package-jpa21-model,
package-jpa22-model, package-xml-extended, package-validation-failed, package-jpa-advanced-field-access, package-jpa-advanced-properties,
package-jpa-pu_with_spaces, package-partitioned, package-plsql, package-jpa-performance, package-jpa-performance2, package-cascade-deletes,
package-jpa-metamodel, package-delimited, package-beanvalidation, package-beanvalidation-dynamic, package-composite-advanced,
package-jpa-metamodel, package-delimited, package-batchfetch, package-beanvalidation, package-beanvalidation-dynamic, package-composite-advanced,
package-composite-advanced-member_1, package-composite-advanced-member_2, package-composite-advanced-member_3, package-xml-composite-advanced,
package-xml-composite-advanced-member_1, package-xml-composite-advanced-member_2, package-xml-composite-advanced-member_3,
package-xml-extended-composite-advanced, package-xml-extended-composite-advanced-member_1, package-xml-extended-composite-advanced-member_2,
Expand Down Expand Up @@ -1385,6 +1387,20 @@
</jar>
</target>

<target name="package-batchfetch" depends="">
<copy todir="${jpatest.basedir}/${build.dir}/${eclipselink.batchfetch.model}/META-INF">
<fileset dir="${jpatest.basedir}/resource/${eclipselink.batchfetch.model}" includes="*.xml"/>
</copy>
<copy todir="${jpatest.basedir}/${build.dir}/${eclipselink.batchfetch.model}">
<fileset dir="${jpatest.basedir}/${classes.dir}"
includes="org/eclipse/persistence/testing/models/jpa/batchfetch/**"/>
</copy>
<jar jarfile="${jpatest.basedir}/${eclipselink.batchfetch.model}.jar">
<fileset dir="${jpatest.basedir}/${build.dir}/${eclipselink.batchfetch.model}">
</fileset>
</jar>
</target>

<target name="package-beanvalidation" depends="">
<copy todir="${jpatest.basedir}/${build.dir}/${eclipselink.beanvalidation.model}/META-INF">
<fileset dir="${jpatest.basedir}/resource/${eclipselink.beanvalidation.model}" includes="*.xml"/>
Expand Down Expand Up @@ -1609,6 +1625,7 @@
<!--pathelement path="${jpatest.basedir}/${eclipselink.pu.with.multibyte}.jar"/-->
<pathelement path="${jpatest.basedir}/${eclipselink.delimited.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.extensibility.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.batchfetch.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.beanvalidation.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.composite.advanced.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.composite.advanced.model.member_1}.jar"/>
Expand Down Expand Up @@ -1646,6 +1663,7 @@
<!--pathelement path="${jpatest.basedir}/${eclipselink.pu.with.multibyte}.jar"/-->
<pathelement path="${jpatest.basedir}/${eclipselink.delimited.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.extensibility.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.batchfetch.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.beanvalidation.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.composite.advanced.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.composite.advanced.model.member_1}.jar"/>
Expand Down Expand Up @@ -1683,6 +1701,7 @@
<!--pathelement path="${jpatest.basedir}/${eclipselink.pu.with.multibyte}.jar"/-->
<pathelement path="${jpatest.basedir}/${eclipselink.delimited.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.extensibility.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.batchfetch.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.beanvalidation.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.composite.advanced.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.composite.advanced.model.member_1}.jar"/>
Expand Down Expand Up @@ -2204,6 +2223,7 @@
<pathelement path="${jpatest.basedir}/woven-${eclipselink.metamodel.model}.jar"/>
<pathelement path="${jpatest.basedir}/woven-${eclipselink.delimited.model}.jar"/>
<pathelement path="${jpatest.basedir}/woven-${eclipselink.extensibility.model}.jar"/>
<pathelement path="${jpatest.basedir}/woven-${eclipselink.batchfetch.model}.jar"/>
<pathelement path="${jpatest.basedir}/woven-${eclipselink.beanvalidation.model}.jar"/>
<pathelement path="${jpatest.basedir}/woven-${eclipselink.beanvalidation.dynamic.model}.jar"/>
<pathelement path="${jpatest.basedir}/${eclipselink.composite.advanced.model}.jar"/>
Expand Down Expand Up @@ -2803,6 +2823,13 @@
<path refid="run.classpath"/>
</classpath>
</weave>
<weave source= "${jpatest.basedir}/${eclipselink.batchfetch.model}.jar"
target = "${jpatest.basedir}/woven-${eclipselink.batchfetch.model}.jar"
loglevel="${logging.level}">
<classpath>
<path refid="run.classpath"/>
</classpath>
</weave>
<weave source= "${jpatest.basedir}/${eclipselink.beanvalidation.model}.jar"
target = "${jpatest.basedir}/woven-${eclipselink.beanvalidation.model}.jar"
loglevel="${logging.level}">
Expand Down Expand Up @@ -3997,6 +4024,7 @@
<target name="server-test-lrg" depends="clean-test">
<antcall target="server-test-advanced" inheritRefs="true"/>
<antcall target="server-test-advanced-multitenant" inheritRefs="true"/>
<antcall target="server-test-batchfetch" inheritRefs="true"/>
<antcall target="server-test-beanvalidation" inheritRefs="true"/>
<antcall target="server-test-cacheable" inheritRefs="true"/>
<antcall target="server-test-cascadedeletes" inheritRefs="true"/>
Expand Down Expand Up @@ -4142,6 +4170,20 @@
*.jdbc"/>
</delete>
</target>

<target name="server-test-batchfetch">
<antcall target="server-run-all" inheritRefs="true">
<param name="PERSISTENCE_UNIT_NAME" value="batchfetch"/>
<param name="MODEL_DIR" value="org/eclipse/persistence/testing/models/jpa/batchfetch"/>
<param name="MODEL_NAME" value="eclipselink-batchfetch-model"/>
<param name="TEST_DIR" value="org/eclipse/persistence/testing/tests/jpa/batchfetch"/>
<param name="TEST_NAME" value="eclipselink-batchfetch-model"/>
<param name="EAR_NAME" value="eclipselink-batchfetch-model"/>
<param name="TEST_SUITE" value="org.eclipse.persistence.testing.tests.jpa.batchfetch.BatchFetchJUnitTest"/>
<param name="eclipselink.jpa.test.dir" value="."/>
</antcall>
</target>

<target name="server-test-beanvalidation">
<antcall target="server-run-all" inheritRefs="true">
<param name="PERSISTENCE_UNIT_NAME" value="beanvalidation"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
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
-->

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd" version="2.0">
<persistence-unit name="batchfetch">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.eclipse.persistence.testing.models.jpa.batchfetch.Company</class>
<class>org.eclipse.persistence.testing.models.jpa.batchfetch.Employee</class>
<class>org.eclipse.persistence.testing.models.jpa.batchfetch.Record</class>
<properties>
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.level.sql" value="FINEST"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
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
-->

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd" version="2.0">
<persistence-unit name="@default@" transaction-type="@transaction-type@">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<@datasource-type@>@data-source-name@</@datasource-type@>
<class>org.eclipse.persistence.testing.models.jpa.batchfetch.Company</class>
<class>org.eclipse.persistence.testing.models.jpa.batchfetch.Employee</class>
<class>org.eclipse.persistence.testing.models.jpa.batchfetch.Record</class>
<properties>
<property name="eclipselink.target-server" value="@server-platform@"/>
<property name="eclipselink.target-database" value="@database-platform@"/>
<property name="eclipselink.weaving" value="@server-weaving@"/>
<property name="eclipselink.logging.logger" value="DefaultLogger"/>
</properties>
</persistence-unit>
</persistence>
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 javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.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;
}
}
Loading

0 comments on commit 2db4798

Please sign in to comment.