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

Add defensive null check to stop null pointer exception #1702

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -86,13 +86,15 @@ public Expression additionalHistoryExpression(Expression context, Expression bas
* @param tableIndex not null indicates that only expression for a single table should be returned.
*/
public Expression additionalHistoryExpression(Expression context, Expression base, Integer tableIndex) {
//
AsOfClause clause = base.getAsOfClause();
Object value = clause.getValue();
Expression join = null;
Expression subJoin = null;
Expression start = null;
Expression end = null;
AsOfClause clause = base.getAsOfClause();
if (clause == null) {
return null;
}
Object value = clause.getValue();
if (value == null) {
return null;
// for now nothing as assume mirroring historical tables.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class DataExpression extends BaseExpression {
protected List<Expression> derivedFields;
protected boolean hasBeenNormalized;
protected TableAliasLookup tableAliases;
protected AsOfClause asOfClause;
protected AsOfClause asOfClause = AsOfClause.NO_CLAUSE;

/**
* DataExpression constructor comment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public List<DatabaseTable> getOwnedTables() {
}
} else if (descriptor.isAggregateDescriptor()) {
return null;
} else if ((descriptor.getHistoryPolicy() != null) && (getAsOfClause().getValue() != null)) {
} else if (descriptor.getHistoryPolicy() != null && getAsOfClause() != null && getAsOfClause().getValue() != null) {
tables = descriptor.getHistoryPolicy().getHistoricalTables();
} else if (isUsingOuterJoinForMultitableInheritance()) {
tables = descriptor.getInheritancePolicy().getAllTables();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.eclipse.persistence.tools.schemaframework.ForeignKeyConstraint;
import org.eclipse.persistence.tools.schemaframework.TableDefinition;

import java.util.Map;

public class AdvancedTableCreator extends TogglingFastTableCreator {
public AdvancedTableCreator() {
setName("EJB3EmployeeProject");
Expand Down Expand Up @@ -107,7 +109,9 @@ public AdvancedTableCreator() {
addTableDefinition(buildRABBITFOOTTable());
addTableDefinition(buildCMP3_HINGETable());//Bug#457480
addTableDefinition(buildCMP3_ROOMTable());
addTableDefinition(buildCMP3_ROOM_HISTTable());
addTableDefinition(buildCMP3_DOORTable());
addTableDefinition(buildCMP3_DOOR_HISTTable());
addTableDefinition(buildCMP3_PRODUCTTable());
addTableDefinition(buildCMP3_CANOETable());
addTableDefinition(buildCMP3_LAKETable());
Expand Down Expand Up @@ -2659,6 +2663,15 @@ public TableDefinition buildCMP3_ROOMTable() {
return table;
}

public TableDefinition buildCMP3_ROOM_HISTTable() {
TableDefinition table = buildCMP3_ROOMTable();
table.setName(table.getName() + "_HIST");
table.addField(buildHistoryField("START_DATE", true, false));
table.addField(buildHistoryField("END_DATE", false, true));
table.setUserDefinedForeignKeyConstraints(Map.of());
return table;
}

public TableDefinition buildCMP3_PRODUCTTable() {
TableDefinition table = new TableDefinition();
table.setName("CMP3_PRODUCT");
Expand Down Expand Up @@ -2815,6 +2828,15 @@ public TableDefinition buildCMP3_DOORTable() {
return table;
}

public TableDefinition buildCMP3_DOOR_HISTTable() {
TableDefinition table = buildCMP3_DOORTable();
table.setName(table.getName() + "_HIST");
table.addField(buildHistoryField("START_DATE", true, false));
table.addField(buildHistoryField("END_DATE", false, true));
table.setUserDefinedForeignKeyConstraints(Map.of());
return table;
}

public TableDefinition buildCMP3_CANOETable() {
TableDefinition table = new TableDefinition();
table.setName("CMP3_CANOE");
Expand Down Expand Up @@ -2953,28 +2975,22 @@ public TableDefinition buildCMP3_PEARLTable() {
public TableDefinition buildCMP3_PEARL_HISTTable() {
TableDefinition table = buildCMP3_PEARLTable();
table.setName(table.getName() + "_HIST");

FieldDefinition fieldSTART = new FieldDefinition();
fieldSTART.setName("START_DATE");
fieldSTART.setTypeName("TIMESTAMP");
fieldSTART.setIsPrimaryKey(true);
fieldSTART.setIsIdentity(false);
fieldSTART.setUnique(false);
fieldSTART.setShouldAllowNull(false);
table.addField(fieldSTART);

FieldDefinition fieldEND = new FieldDefinition();
fieldEND.setName("END_DATE");
fieldEND.setTypeName("TIMESTAMP");
fieldEND.setIsPrimaryKey(false);
fieldEND.setIsIdentity(false);
fieldEND.setUnique(false);
fieldEND.setShouldAllowNull(true);
table.addField(fieldEND);

table.addField(buildHistoryField("START_DATE", true, false));
table.addField(buildHistoryField("END_DATE", false, true));
return table;
}

private static FieldDefinition buildHistoryField(String name, boolean isPrimaryKey, boolean allowNull) {
FieldDefinition field = new FieldDefinition();
field.setName(name);
field.setTypeName("TIMESTAMP");
field.setIsPrimaryKey(isPrimaryKey);
field.setIsIdentity(false);
field.setUnique(false);
field.setShouldAllowNull(allowNull);
return field;
}

public TableDefinition buildJobTable() {
TableDefinition table = new TableDefinition();
table.setName("JOB");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import org.eclipse.persistence.annotations.Customizer;
import org.eclipse.persistence.annotations.Mutable;
import org.eclipse.persistence.annotations.ReadTransformer;
import org.eclipse.persistence.annotations.WriteTransformer;
Expand All @@ -36,6 +37,7 @@

@Entity
@Table(name="CMP3_DOOR")
@Customizer(AdvancedHistoryCustomizer.class)
public class Door implements Serializable, Cloneable {
@Id
private int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.QueryHint;
import jakarta.persistence.Table;
import org.eclipse.persistence.annotations.Customizer;
import org.eclipse.persistence.config.QueryHints;

import java.io.Serializable;
Expand All @@ -42,6 +43,7 @@
@QueryHint(name = QueryHints.QUERY_RESULTS_CACHE, value = "true"),
})
})
@Customizer(AdvancedHistoryCustomizer.class)
public class Room implements Serializable, Cloneable {
@Id
private int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public static Test suite() {
suite.addTest(new AdvancedJPAJunitTest("testTransparentIndirectionValueHolderSessionReset"));
suite.addTest(new AdvancedJPAJunitTest("testTransparentIndirectionQuerySessionReset"));
suite.addTest(new AdvancedJPAJunitTest("testHistoryRelationshipQueryInitialization"));
suite.addTest(new AdvancedJPAJunitTest("testQueryJoinTablesWithHistory"));
return suite;
}

Expand Down Expand Up @@ -2827,7 +2828,23 @@ public void testJoinFetchWithRefreshOnRelatedEntity() {
closeEntityManager(em);
}
}


/**
* Bug 526836
* NPE when joining tables that have a history policy applied.
* Internal Exception: java.lang.NullPointerException: Cannot invoke
* "org.eclipse.persistence.history.AsOfClause.getValue()" because the return value of
* "org.eclipse.persistence.internal.expressions.ObjectExpression.getAsOfClause()" is null
*/
public void testQueryJoinTablesWithHistory() {

EntityManager em = createEntityManager();

// Previously would throw a null pointer exception if both tables had a history
// policy applied like @Customizer(AdvancedHistoryCustomizer.class)
em.createQuery("select d from Door d left join Room r on d.room = r");
}

/**
* Bug 464088
* Test non-historized Entity eager-referencing a historized Entity's queries function correctly
Expand Down