Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.7] Fix for ReportQuery not fetching relationships with BatchFetch.IN #2316

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;
rfelcman marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
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;
}
}
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_RECORD")
public class Record {
@Id
private long id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "EMPLOYEE_ID")
@BatchFetch(value = BatchFetchType.IN)
private Employee employee;

public Record() {
}

public Record(long id, Employee employee) {
this.id = id;
this.employee = employee;
}

public long getId() {
return id;
}

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

public Employee getEmployee() {
return employee;
}

public void setEmployee(Employee employee) {
this.employee = employee;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.eclipse.persistence.testing.tests.jpa.advanced.fetchgroup.AdvancedFetchGroupJunitTest;
import org.eclipse.persistence.testing.tests.jpa.advanced.multitenant.AdvancedMultiTenantJunitTest;
import org.eclipse.persistence.testing.tests.jpa.advanced.multitenant.AdvancedMultiTenantSchemaJunitTest;
import org.eclipse.persistence.testing.tests.jpa.batchfetch.BatchFetchJUnitTest;
import org.eclipse.persistence.testing.tests.jpa.cacheable.CacheableModelJunitTest;
import org.eclipse.persistence.testing.tests.jpa.cacheable.CacheableModelJunitTestEnableSelective;
import org.eclipse.persistence.testing.tests.jpa.cascadedeletes.CascadeDeletesJUnitTestSuite;
Expand Down Expand Up @@ -373,6 +374,9 @@ public static TestSuite suite4() {
// Persistence Unit Processor tests.
fullSuite.addTest(PersistenceUnitProcessorTest.suite());

// Batchfetch tests
fullSuite.addTest(BatchFetchJUnitTest.suite());

return fullSuite;
}
}
Loading
Loading