Skip to content
Merged
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 @@ -76,4 +76,16 @@ public int hashCode() {
public String toString() {
return entity.getName() + " " + changeId;
}

protected boolean entitiesHaveSameNameAndDataMap(DbRowOp rowOp) {
return entitiesHaveSameName(rowOp) && entitiesHaveSameDataMap(rowOp);
}

boolean entitiesHaveSameName(DbRowOp rowOp) {
return rowOp.getEntity().getName().equals(getEntity().getName());
}

private boolean entitiesHaveSameDataMap(DbRowOp rowOp) {
return rowOp.getEntity().getDataMap().getName().equals(getEntity().getDataMap().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public boolean isSameBatch(DbRowOp rowOp) {
if(!(rowOp instanceof DeleteDbRowOp)) {
return false;
}
if(!rowOp.getEntity().getName().equals(getEntity().getName())) {
if(!entitiesHaveSameNameAndDataMap(rowOp)) {
return false;
}
DeleteDbRowOp other = (DeleteDbRowOp) rowOp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean isSameBatch(DbRowOp rowOp) {
if(!(rowOp instanceof InsertDbRowOp)) {
return false;
}
return rowOp.getEntity().getName().equals(getEntity().getName());
return entitiesHaveSameNameAndDataMap(rowOp);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean isSameBatch(DbRowOp rowOp) {
if(!(rowOp instanceof UpdateDbRowOp)) {
return false;
}
if(!rowOp.getEntity().getName().equals(getEntity().getName())) {
if(!entitiesHaveSameNameAndDataMap(rowOp)) {
return false;
}
UpdateDbRowOp other = (UpdateDbRowOp) rowOp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.cayenne.access.flush.operation.DeleteDbRowOp;
import org.apache.cayenne.access.flush.operation.InsertDbRowOp;
import org.apache.cayenne.access.flush.operation.UpdateDbRowOp;
import org.apache.cayenne.map.DataMap;
import org.apache.cayenne.map.DbAttribute;
import org.apache.cayenne.map.DbEntity;
import org.apache.cayenne.query.DeleteBatchQuery;
Expand Down Expand Up @@ -168,6 +169,45 @@ public void createQueries() {
assertEquals(1, delete2.getRows().size());
}

@Test
public void dontMergeSameTableNameOnDifferentDataMaps() {
DbEntity test1Datamap1 = mockEntity("test1", "datamap1");
DbEntity test1Datamap2 = mockEntity("test1", "datamap2");
DbEntity test2Datamap1 = mockEntity("test2", "datamap1");
DbEntity test2Datamap2 = mockEntity("test2", "datamap2");
DbEntity test3Datamap1 = mockEntity("test3", "datamap1");
DbEntity test3Datamap2 = mockEntity("test3", "datamap2");

ObjectId id1 = ObjectId.of("test1", "id", 1);
ObjectId id2 = ObjectId.of("test1", "id", 2);
ObjectId id3 = ObjectId.of("test2", "id", 3);
ObjectId id4 = ObjectId.of("test2", "id", 4);
ObjectId id5 = ObjectId.of("test3", "id", 5);
ObjectId id6 = ObjectId.of("test3", "id", 6);

UpdateDbRowOp update1 = new UpdateDbRowOp(mockObject(id3), test2Datamap1, id3);
update1.getValues().addValue(new DbAttribute("attr"), "abc", false);
update1.getValues().addValue(new DbAttribute("attr"), "abc", false);

UpdateDbRowOp update2 = new UpdateDbRowOp(mockObject(id4), test2Datamap2, id4);
update2.getValues().addValue(new DbAttribute("attr"), "def", false);
update2.getValues().addValue(new DbAttribute("attr"), "def", false);
List<DbRowOp> ops = List.of(
new InsertDbRowOp(mockObject(id1), test1Datamap1, id1),
new InsertDbRowOp(mockObject(id2), test1Datamap2, id2),
update1,
update2,
new DeleteDbRowOp(mockObject(id5), test3Datamap1, id5),
new DeleteDbRowOp(mockObject(id6), test3Datamap2, id6)
);

DefaultDataDomainFlushAction action = mock(DefaultDataDomainFlushAction.class);
when(action.createQueries((List<DbRowOp>) any(List.class))).thenCallRealMethod();

List<? extends Query> queries = action.createQueries(ops);
assertEquals(6, queries.size());
}

private Persistent mockObject(ObjectId id) {
Persistent persistent = mock(Persistent.class);
when(persistent.getObjectId()).thenReturn(id);
Expand All @@ -176,12 +216,17 @@ private Persistent mockObject(ObjectId id) {
}

private DbEntity mockEntity(String name) {
return mockEntity(name, "defaultMap");
}

private DbEntity mockEntity(String name, String datamapName) {
DbAttribute attribute1 = new DbAttribute("id");
attribute1.setPrimaryKey(true);
DbAttribute attribute2 = new DbAttribute("attr");
DbEntity testEntity = new DbEntity(name);
testEntity.addAttribute(attribute1);
testEntity.addAttribute(attribute2);
testEntity.setDataMap(new DataMap(datamapName));
return testEntity;
}
}