File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed
objectbox-java/src/main/java/io/objectbox/relation
tests/objectbox-java-test/src/main/java/io/objectbox/relation Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -327,6 +327,26 @@ public synchronized boolean remove(Object object) {
327
327
return removed ;
328
328
}
329
329
330
+ @ Beta
331
+ /** Removes an object by its entity ID. */
332
+ public synchronized TARGET removeById (long id ) {
333
+ ensureEntities ();
334
+ int size = entities .size ();
335
+ IdGetter <TARGET > idGetter = relationInfo .targetInfo .getIdGetter ();
336
+ for (int i = 0 ; i < size ; i ++) {
337
+ TARGET candidate = entities .get (i );
338
+ if (idGetter .getId (candidate ) == id ) {
339
+ TARGET removed = remove (i );
340
+ if (removed != candidate ) {
341
+ throw new IllegalStateException ("Mismatch: " + removed + " vs. " + candidate );
342
+ }
343
+ return candidate ;
344
+ }
345
+
346
+ }
347
+ return null ;
348
+ }
349
+
330
350
@ Override
331
351
public synchronized boolean removeAll (Collection <?> objects ) {
332
352
boolean changes = false ;
@@ -518,7 +538,7 @@ public boolean hasA(QueryFilter<TARGET> filter) {
518
538
public boolean hasAll (QueryFilter <TARGET > filter ) {
519
539
ensureEntities ();
520
540
Object [] objects = entities .toArray ();
521
- if (objects .length == 0 ) {
541
+ if (objects .length == 0 ) {
522
542
return false ;
523
543
}
524
544
for (Object target : objects ) {
Original file line number Diff line number Diff line change @@ -187,6 +187,20 @@ public void testRemoveAll() {
187
187
assertOrder2And4Removed (count , customer , toMany );
188
188
}
189
189
190
+ @ Test
191
+ public void testRemoveById () {
192
+ int count = 5 ;
193
+ Customer customer = putCustomerWithOrders (count );
194
+ ToMany <Order > toMany = (ToMany <Order >) customer .orders ;
195
+ Order removed1 = toMany .removeById (toMany .get (3 ).getId ());
196
+ assertEquals ("order4" , removed1 .getText ());
197
+ Order removed2 = toMany .removeById (toMany .get (1 ).getId ());
198
+ assertEquals ("order2" , removed2 .getText ());
199
+ assertNull (toMany .removeById (42 ));
200
+ customerBox .put (customer );
201
+ assertOrder2And4Removed (count , customer , toMany );
202
+ }
203
+
190
204
@ Test
191
205
public void testRetainAll () {
192
206
int count = 5 ;
You can’t perform that action at this time.
0 commit comments