Skip to content

Commit 380cbb3

Browse files
committed
Bug fix: When saving an entity, that was saved with an aggregate object that contains null values, with a null value for an aggregate then EclipseLink will still generate an update record for that record field changing it from NULL to NULL, but it should not do that as there was no change.
1 parent 2637c10 commit 380cbb3

File tree

6 files changed

+301
-1
lines changed

6 files changed

+301
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.eclipse.persistence.testing.models.aggregate;
2+
3+
public class SimpleAggregate {
4+
private String content;
5+
6+
public String getContent() {
7+
return content;
8+
}
9+
10+
public void setContent(String content) {
11+
this.content = content;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.eclipse.persistence.testing.models.aggregate;
2+
3+
import org.eclipse.persistence.descriptors.ChangedFieldsLockingPolicy;
4+
import org.eclipse.persistence.descriptors.ClassDescriptor;
5+
import org.eclipse.persistence.descriptors.RelationalDescriptor;
6+
import org.eclipse.persistence.mappings.AggregateObjectMapping;
7+
import org.eclipse.persistence.mappings.DirectToFieldMapping;
8+
9+
public class SimpleAggregateProject extends org.eclipse.persistence.sessions.Project {
10+
11+
public SimpleAggregateProject() {
12+
applyPROJECT();
13+
applyLOGIN();
14+
15+
addDescriptor(buildSimpleAggregateClassDescriptor());
16+
addDescriptor(buildSimpleEntityDescriptor());
17+
}
18+
19+
protected void applyLOGIN() {
20+
}
21+
22+
protected void applyPROJECT() {
23+
setName("SimpleAggregateSystem");
24+
}
25+
26+
public ClassDescriptor buildSimpleAggregateClassDescriptor() {
27+
RelationalDescriptor descriptor = new RelationalDescriptor();
28+
descriptor.descriptorIsAggregate();
29+
descriptor.setJavaClass(SimpleAggregate.class);
30+
31+
// ClassDescriptor Properties.
32+
descriptor.setAlias("SimpleAggregate");
33+
34+
35+
// Query Manager.
36+
37+
38+
// Event Manager.
39+
40+
// Mappings.
41+
DirectToFieldMapping contentMapping = new DirectToFieldMapping();
42+
contentMapping.setAttributeName("content");
43+
contentMapping.setFieldName("content->DIRECT");
44+
descriptor.addMapping(contentMapping);
45+
46+
return descriptor;
47+
}
48+
49+
protected ClassDescriptor buildSimpleEntityDescriptor() {
50+
RelationalDescriptor descriptor = new RelationalDescriptor();
51+
//descriptor.setCacheable(false);
52+
53+
// SECTION: DESCRIPTOR
54+
descriptor.setJavaClass(SimpleEntity.class);
55+
descriptor.addTableName("SIMPLEENTITY");
56+
descriptor.addPrimaryKeyFieldName("SIMPLEENTITY.ID");
57+
58+
ChangedFieldsLockingPolicy lockingPolicy = new ChangedFieldsLockingPolicy();
59+
descriptor.setOptimisticLockingPolicy(lockingPolicy);
60+
61+
// SECTION: DIRECTTOFIELDMAPPING
62+
DirectToFieldMapping directtofieldmapping = new DirectToFieldMapping();
63+
directtofieldmapping.setAttributeName("id");
64+
directtofieldmapping.setIsReadOnly(false);
65+
directtofieldmapping.setFieldName("SIMPLEENTITY.ID");
66+
descriptor.addMapping(directtofieldmapping);
67+
68+
DirectToFieldMapping fieldmapping = new DirectToFieldMapping();
69+
fieldmapping.setAttributeName("field");
70+
fieldmapping.setIsReadOnly(false);
71+
fieldmapping.setFieldName("SIMPLEENTITY.FIELD");
72+
descriptor.addMapping(fieldmapping);
73+
74+
AggregateObjectMapping aggregateObjectMapping = new AggregateObjectMapping();
75+
aggregateObjectMapping.setAttributeName("simpleAggregate");
76+
aggregateObjectMapping.setReferenceClass(SimpleAggregate.class);
77+
aggregateObjectMapping.setIsNullAllowed(true);
78+
aggregateObjectMapping.addFieldNameTranslation("SIMPLEENTITY.CONTENT", "content->DIRECT");
79+
descriptor.addMapping(aggregateObjectMapping);
80+
81+
return descriptor;
82+
}
83+
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
// Contributors:
14+
// Oracle - initial API and implementation from Oracle TopLink
15+
package org.eclipse.persistence.testing.models.aggregate;
16+
17+
import org.eclipse.persistence.queries.ReadObjectQuery;
18+
import org.eclipse.persistence.sessions.DatabaseSession;
19+
import org.eclipse.persistence.sessions.UnitOfWork;
20+
import org.eclipse.persistence.testing.framework.TestSystem;
21+
import org.eclipse.persistence.testing.tests.dynamic.QuerySQLTracker;
22+
import org.eclipse.persistence.tools.schemaframework.SchemaManager;
23+
24+
public class SimpleAggregateSystem extends TestSystem {
25+
26+
public SimpleAggregateSystem() {
27+
project = new SimpleAggregateProject();
28+
}
29+
30+
@Override
31+
public void addDescriptors(DatabaseSession session) {
32+
if (project == null) {
33+
project = new SimpleAggregateProject();
34+
}
35+
session.addDescriptors(project);
36+
}
37+
38+
@Override
39+
public void createTables(DatabaseSession session) {
40+
SchemaManager schemaManager = new SchemaManager(session);
41+
42+
schemaManager.replaceObject(SimpleEntity.tableDefinition());
43+
}
44+
45+
@Override
46+
public void populate(DatabaseSession session) {
47+
48+
QuerySQLTracker qTracker = QuerySQLTracker.install(session);
49+
50+
{
51+
UnitOfWork uow = session.acquireUnitOfWork();
52+
53+
SimpleEntity instance = new SimpleEntity();
54+
instance.setId("1");
55+
instance.setField("constant");
56+
instance.setSimpleAggregate(new SimpleAggregate());
57+
session.writeObject(instance);
58+
uow.registerObject(instance);
59+
uow.commit();
60+
}
61+
62+
{
63+
UnitOfWork uow = session.acquireUnitOfWork();
64+
65+
ReadObjectQuery query = new ReadObjectQuery();
66+
final SimpleEntity queryInstance = new SimpleEntity();
67+
queryInstance.setId("1");
68+
query.setSelectionObject(queryInstance);
69+
final SimpleEntity instance = (SimpleEntity) uow.executeQuery(query);
70+
if (instance == null) {
71+
throw new RuntimeException("Object was not found");
72+
}
73+
if (instance.getSimpleAggregate() == null) {
74+
//throw new RuntimeException("SimpleAggregate is null");
75+
}
76+
instance.setSimpleAggregate(instance.getSimpleAggregate() == null ? new SimpleAggregate() : null);
77+
78+
qTracker.reset();
79+
80+
instance.setSimpleAggregate(null);
81+
uow.registerObject(instance);
82+
uow.commit();
83+
84+
if (qTracker.getQueries().size() > 0) {
85+
throw new RuntimeException("Unexpected query was executed: " + qTracker.getQueries());
86+
}
87+
}
88+
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.eclipse.persistence.testing.models.aggregate;
2+
3+
import org.eclipse.persistence.tools.schemaframework.TableDefinition;
4+
5+
public class SimpleEntity {
6+
private String id;
7+
private String field;
8+
private SimpleAggregate simpleAggregate;
9+
10+
public String getId() {
11+
return id;
12+
}
13+
14+
public void setId(String id) {
15+
this.id = id;
16+
}
17+
18+
public String getField() {
19+
return field;
20+
}
21+
22+
public void setField(String field) {
23+
this.field = field;
24+
}
25+
26+
public SimpleAggregate getSimpleAggregate() {
27+
return simpleAggregate;
28+
}
29+
30+
public void setSimpleAggregate(SimpleAggregate simpleAggregate) {
31+
this.simpleAggregate = simpleAggregate;
32+
}
33+
34+
public static TableDefinition tableDefinition() {
35+
TableDefinition definition = new TableDefinition();
36+
37+
definition.setName("SIMPLEENTITY");
38+
39+
definition.addIdentityField("ID", String.class, 15);
40+
definition.addField("FIELD", String.class, 15);
41+
definition.addField("CONTENT", String.class, 20);
42+
43+
return definition;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.eclipse.persistence.testing.tests.aggregate;
2+
3+
import org.eclipse.persistence.testing.framework.TestModel;
4+
import org.eclipse.persistence.testing.framework.TestSuite;
5+
import org.eclipse.persistence.testing.models.aggregate.SimpleAggregateSystem;
6+
7+
public class SimpleAggregateTestModel extends TestModel {
8+
public SimpleAggregateTestModel() {
9+
setDescription("This model tests reading/writing/deleting of the simple aggregate model.");
10+
}
11+
12+
public void addForcedRequiredSystems() {
13+
//We need to ensure that the correct database schema is created
14+
addForcedRequiredSystem(new SimpleAggregateSystem());
15+
}
16+
17+
public void addRequiredSystems() {
18+
}
19+
20+
public void addTests() {
21+
addTest(getUpdateObjectTestSuite());
22+
}
23+
24+
public static TestSuite getUpdateObjectTestSuite() {
25+
TestSuite suite = new TestSuite();
26+
suite.setName("AggregateUpdateObjectTestSuite");
27+
suite.setDescription("This suite tests the updating of each object in the aggregate model.");
28+
29+
return suite;
30+
}
31+
32+
public static junit.framework.TestSuite suite() {
33+
return new SimpleAggregateTestModel();
34+
}
35+
}

foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/mappings/AggregateMapping.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public ChangeRecord compareForChange(Object clone, Object backup, ObjectChangeSe
316316

317317
if (!owner.isNew()) {
318318
backupAttribute = getAttributeValueFromObject(backup);
319-
if ((cloneAttribute == null) && (backupAttribute == null)) {
319+
if (checkNull(cloneAttribute) && checkNull(backupAttribute)) {
320320
return null;// no change
321321
}
322322
if ((cloneAttribute != null) && (backupAttribute != null) && (!cloneAttribute.getClass().equals(backupAttribute.getClass()))) {
@@ -354,6 +354,39 @@ public ChangeRecord compareForChange(Object clone, Object backup, ObjectChangeSe
354354
return changeRecord;
355355
}
356356

357+
public static Set<java.lang.reflect.Field> getAllFields(final Set<java.lang.reflect.Field> fields, final Class<?> type) {
358+
final java.lang.reflect.Field[] declaredFields = type.getDeclaredFields();
359+
for (final java.lang.reflect.Field declaredField : declaredFields) {
360+
if (!java.lang.reflect.Modifier.isStatic(declaredField.getModifiers())) {
361+
fields.add(declaredField);
362+
}
363+
}
364+
365+
if (type.getSuperclass() != null) {
366+
getAllFields(fields, type.getSuperclass());
367+
}
368+
369+
return fields;
370+
}
371+
372+
public static boolean checkNull(final Object o) {
373+
if (o == null) {
374+
return true;
375+
}
376+
final Set<java.lang.reflect.Field> fields = getAllFields(new HashSet<java.lang.reflect.Field>(), o.getClass());
377+
for (final java.lang.reflect.Field f : fields) {
378+
f.setAccessible(true);
379+
try {
380+
final Object objectField = f.get(o);
381+
if (objectField != null)
382+
return false;
383+
} catch (IllegalAccessException e) {
384+
throw new RuntimeException(e);
385+
}
386+
}
387+
return true;
388+
}
389+
357390
/**
358391
* INTERNAL:
359392
* Compare the attributes belonging to this mapping for the objects.

0 commit comments

Comments
 (0)