Skip to content

Commit

Permalink
Fix for ReportQuery not fetching relationships with BatchFetch.IN (#2310
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Sheikah45 authored Nov 22, 2024
1 parent 2f32392 commit c812e66
Show file tree
Hide file tree
Showing 11 changed files with 621 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,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 @@ -251,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<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
82 changes: 82 additions & 0 deletions jpa/eclipselink.jpa.testapps/jpa.test.batchfetch/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!--
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>
<artifactId>org.eclipse.persistence.jpa.testapps</artifactId>
<groupId>org.eclipse.persistence</groupId>
<version>4.0.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>org.eclipse.persistence.jpa.testapps.batchfetch</artifactId>

<name>Test - batchfetch</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<!--Resolve dependencies into Maven properties like ${org.eclipse.persistence:org.eclipse.persistence.jpa:jar} for JPA module-->
<execution>
<id>get-test-classpath-to-properties</id>
<phase>process-test-classes</phase>
</execution>
</executions>
</plugin>
<!-- start/stop in-memory derby before/after the test -->
<plugin>
<groupId>org.carlspring.maven</groupId>
<artifactId>derby-maven-plugin</artifactId>
<executions>
<execution>
<id>start-derby</id>
<phase>process-test-classes</phase>
</execution>
<execution>
<id>stop-derby</id>
<phase>prepare-package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<configuration>
<!-- turn on dynamic weaving -->
<argLine>-javaagent:${org.eclipse.persistence:org.eclipse.persistence.jpa:jar}</argLine>
</configuration>
</execution>
<execution>
<id>server-test</id>
<configuration>
<!-- exclude some tests from server-side run -->
<excludes>
<exclude>**/EntityManagerImplTest</exclude>
<exclude>**/EntityManagerFactoryImplTest</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
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,52 @@
/*
* 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 java.util.Objects;

public final class Count {
private final long value;
private final Employee employee;

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

public long value() {return value;}

public Employee employee() {return employee;}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (Count) obj;
return this.value == that.value &&
Objects.equals(this.employee, that.employee);
}

@Override
public int hashCode() {
return Objects.hash(value, employee);
}

@Override
public String toString() {
return "Count[" +
"value=" + value + ", " +
"employee=" + employee + ']';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import org.eclipse.persistence.annotations.BatchFetch;
import org.eclipse.persistence.annotations.BatchFetchType;

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

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "COMPANY_ID")
@BatchFetch(value = BatchFetchType.IN)
private Company company;

public Employee() {
}

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

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}
}
Loading

0 comments on commit c812e66

Please sign in to comment.