Skip to content

Commit 417c2df

Browse files
committed
ToMany: minor improvements for removed entities with ID 0, etc.
1 parent 421745a commit 417c2df

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

  • objectbox-java/src/main/java/io/objectbox/relation
  • tests/objectbox-java-test/src/main/java/io/objectbox/relation

objectbox-java/src/main/java/io/objectbox/relation/ToMany.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class ToMany<TARGET> implements List<TARGET>, Serializable {
8181
private Map<TARGET, Boolean> entitiesRemoved;
8282

8383
List<TARGET> entitiesToPut;
84-
List<TARGET> entitiesToRemove;
84+
List<TARGET> entitiesToRemoveFromDb;
8585

8686
transient private BoxStore boxStore;
8787
transient private Box entityBox;
@@ -241,6 +241,7 @@ private void trackRemove(TARGET object) {
241241
if (count == 1) {
242242
entityCounts.remove(object);
243243
entitiesAdded.remove(object);
244+
244245
entitiesRemoved.put(object, TRUE);
245246
} else if (count > 1) {
246247
entityCounts.put(object, count - 1);
@@ -288,7 +289,7 @@ public synchronized void clear() {
288289
}
289290

290291
Map entityCountsToClear = this.entityCounts;
291-
if(entityCountsToClear != null) {
292+
if (entityCountsToClear != null) {
292293
entityCountsToClear.clear();
293294
}
294295
}
@@ -472,7 +473,7 @@ public synchronized void reset() {
472473
entities = null;
473474
entitiesAdded = null;
474475
entitiesRemoved = null;
475-
entitiesToRemove = null;
476+
entitiesToRemoveFromDb = null;
476477
entitiesToPut = null;
477478
entityCounts = null;
478479
}
@@ -652,7 +653,7 @@ public boolean internalCheckApplyToDbRequired() {
652653
synchronized (this) {
653654
if (entitiesToPut == null) {
654655
entitiesToPut = new ArrayList<>();
655-
entitiesToRemove = new ArrayList<>();
656+
entitiesToRemoveFromDb = new ArrayList<>();
656657
}
657658
}
658659

@@ -701,17 +702,19 @@ private boolean prepareBacklinkEntitiesForDb() {
701702
ToOne<Object> toOne = backlinkToOneGetter.getToOne(target);
702703
long toOneTargetId = toOne.getTargetId();
703704
if (toOneTargetId == entityId) {
704-
toOne.setTarget(null);
705-
if (removeFromTargetBox) {
706-
entitiesToRemove.add(target);
707-
} else {
708-
entitiesToPut.add(target);
705+
toOne.setTarget(null); // This is also done for non-persisted entities (if used elsewhere)
706+
if (idGetter.getId(target) != 0) { // No further action for non-persisted entities required
707+
if (removeFromTargetBox) {
708+
entitiesToRemoveFromDb.add(target);
709+
} else {
710+
entitiesToPut.add(target);
711+
}
709712
}
710713
}
711714
}
712715
setRemoved.clear();
713716
}
714-
return !entitiesToPut.isEmpty() || !entitiesToRemove.isEmpty();
717+
return !entitiesToPut.isEmpty() || !entitiesToRemoveFromDb.isEmpty();
715718
}
716719
}
717720

@@ -721,7 +724,7 @@ private boolean prepareBacklinkEntitiesForDb() {
721724
*/
722725
@Internal
723726
public void internalApplyToDb(Cursor sourceCursor, Cursor<TARGET> targetCursor) {
724-
TARGET[] toRemove;
727+
TARGET[] toRemoveFromDb;
725728
TARGET[] toPut;
726729
TARGET[] addedStandalone = null;
727730
TARGET[] removedStandalone = null;
@@ -736,7 +739,7 @@ public void internalApplyToDb(Cursor sourceCursor, Cursor<TARGET> targetCursor)
736739
}
737740
}
738741
if (removeFromTargetBox) {
739-
entitiesToRemove.addAll(entitiesRemoved.keySet());
742+
entitiesToRemoveFromDb.addAll(entitiesRemoved.keySet());
740743
}
741744
if (!entitiesAdded.isEmpty()) {
742745
addedStandalone = (TARGET[]) entitiesAdded.keySet().toArray();
@@ -748,16 +751,18 @@ public void internalApplyToDb(Cursor sourceCursor, Cursor<TARGET> targetCursor)
748751
}
749752
}
750753

751-
toRemove = entitiesToRemove.isEmpty() ? null : (TARGET[]) entitiesToRemove.toArray();
752-
entitiesToRemove.clear();
754+
toRemoveFromDb = entitiesToRemoveFromDb.isEmpty() ? null : (TARGET[]) entitiesToRemoveFromDb.toArray();
755+
entitiesToRemoveFromDb.clear();
753756
toPut = entitiesToPut.isEmpty() ? null : (TARGET[]) entitiesToPut.toArray();
754757
entitiesToPut.clear();
755758
}
756759

757-
if (toRemove != null) {
758-
for (TARGET target : toRemove) {
760+
if (toRemoveFromDb != null) {
761+
for (TARGET target : toRemoveFromDb) {
759762
long id = targetIdGetter.getId(target);
760-
targetCursor.deleteEntity(id);
763+
if (id != 0) {
764+
targetCursor.deleteEntity(id);
765+
}
761766
}
762767
}
763768
if (toPut != null) {

tests/objectbox-java-test/src/main/java/io/objectbox/relation/ToManyTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ public void testAddRemoved() {
267267
assertEquals(count, orderBox.count());
268268
}
269269

270+
@Test
271+
public void testAddRemove() {
272+
Customer customer = putCustomer();
273+
ToMany<Order> toMany = (ToMany<Order>) customer.orders;
274+
Order order = new Order();
275+
toMany.add(order);
276+
toMany.remove(order);
277+
278+
toMany.applyChangesToDb();
279+
assertEquals(0, orderBox.count());
280+
}
281+
270282
@Test
271283
public void testAddAddRemove() {
272284
Customer customer = putCustomer();

0 commit comments

Comments
 (0)