28
28
import javax .annotation .Nullable ;
29
29
import javax .annotation .concurrent .ThreadSafe ;
30
30
31
+ import io .objectbox .annotation .Id ;
31
32
import io .objectbox .annotation .apihint .Beta ;
32
33
import io .objectbox .annotation .apihint .Experimental ;
33
34
import io .objectbox .annotation .apihint .Internal ;
38
39
import io .objectbox .query .QueryBuilder ;
39
40
import io .objectbox .query .QueryCondition ;
40
41
import io .objectbox .relation .RelationInfo ;
42
+ import io .objectbox .relation .ToMany ;
43
+ import io .objectbox .relation .ToOne ;
41
44
42
45
/**
43
46
* A Box to put and get Objects of a specific Entity class.
@@ -335,11 +338,24 @@ public boolean contains(long id) {
335
338
}
336
339
337
340
/**
338
- * Puts the given object in the box (aka persisting it). If this is a new entity (its ID property is 0), a new ID
339
- * will be assigned to the entity (and returned). If the entity was already put in the box before, it will be
340
- * overwritten.
341
+ * Puts the given object and returns its (new) ID.
341
342
* <p>
342
- * Performance note: if you want to put several entities, consider {@link #put(Collection)},
343
+ * This means that if its {@link Id @Id} property is 0 or null, it is inserted as a new object and assigned the next
344
+ * available ID. For example, if there is an object with ID 1 and another with ID 100, it will be assigned ID 101.
345
+ * The new ID is also set on the given object before this returns.
346
+ * <p>
347
+ * If instead the object has an assigned ID set, if an object with the same ID exists it will be updated.
348
+ * Otherwise, it will be inserted with that ID.
349
+ * <p>
350
+ * If the ID was not assigned before an {@link IllegalArgumentException} is thrown.
351
+ * <p>
352
+ * When the object contains {@link ToOne} or {@link ToMany} relations, they are created (or updated) to point to the
353
+ * (new) target objects.
354
+ * The target objects themselves are not updated or removed. To do so, put or remove them using their box.
355
+ * However, for convenience, if a target object is new, it will be inserted and assigned an ID in its box before
356
+ * creating or updating the relation.
357
+ * <p>
358
+ * Performance note: if you want to put several objects, consider {@link #put(Collection)},
343
359
* {@link #put(Object[])}, {@link BoxStore#runInTx(Runnable)}, etc. instead.
344
360
*/
345
361
public long put (T entity ) {
@@ -432,9 +448,11 @@ public void putBatched(@Nullable Collection<T> entities, int batchSize) {
432
448
}
433
449
434
450
/**
435
- * Removes (deletes) the Object by its ID.
451
+ * Removes (deletes) the object with the given ID.
452
+ * <p>
453
+ * If the object is part of a relation, it will be removed from that relation as well.
436
454
*
437
- * @return true if an entity was actually removed (false if no entity exists with the given ID)
455
+ * @return true if the object did exist and was removed, otherwise false.
438
456
*/
439
457
public boolean remove (long id ) {
440
458
Cursor <T > cursor = getWriter ();
@@ -449,7 +467,7 @@ public boolean remove(long id) {
449
467
}
450
468
451
469
/**
452
- * Removes (deletes) Objects by their ID in a single transaction.
470
+ * Like {@link #remove(long)}, but removes multiple objects in a single transaction.
453
471
*/
454
472
public void remove (@ Nullable long ... ids ) {
455
473
if (ids == null || ids .length == 0 ) {
@@ -476,7 +494,7 @@ public void removeByKeys(@Nullable Collection<Long> ids) {
476
494
}
477
495
478
496
/**
479
- * Due to type erasure collision, we cannot simply use "remove" as a method name here .
497
+ * Like {@link #remove(long)}, but removes multiple objects in a single transaction .
480
498
*/
481
499
public void removeByIds (@ Nullable Collection <Long > ids ) {
482
500
if (ids == null || ids .isEmpty ()) {
@@ -494,9 +512,7 @@ public void removeByIds(@Nullable Collection<Long> ids) {
494
512
}
495
513
496
514
/**
497
- * Removes (deletes) the given Object.
498
- *
499
- * @return true if an entity was actually removed (false if no entity exists with the given ID)
515
+ * Like {@link #remove(long)}, but obtains the ID from the {@link Id @Id} property of the given object instead.
500
516
*/
501
517
public boolean remove (T object ) {
502
518
Cursor <T > cursor = getWriter ();
@@ -512,7 +528,7 @@ public boolean remove(T object) {
512
528
}
513
529
514
530
/**
515
- * Removes (deletes) the given Objects in a single transaction.
531
+ * Like {@link #remove(Object)}, but removes multiple objects in a single transaction.
516
532
*/
517
533
@ SafeVarargs // Not using T... as Object[], no ClassCastException expected.
518
534
@ SuppressWarnings ("Duplicates" ) // Detected duplicate has different type
@@ -533,7 +549,7 @@ public final void remove(@Nullable T... objects) {
533
549
}
534
550
535
551
/**
536
- * Removes (deletes) the given Objects in a single transaction.
552
+ * Like {@link #remove(Object)}, but removes multiple objects in a single transaction.
537
553
*/
538
554
@ SuppressWarnings ("Duplicates" ) // Detected duplicate has different type
539
555
public void remove (@ Nullable Collection <T > objects ) {
@@ -553,7 +569,7 @@ public void remove(@Nullable Collection<T> objects) {
553
569
}
554
570
555
571
/**
556
- * Removes (deletes) ALL Objects in a single transaction.
572
+ * Like {@link #remove(long)}, but removes <b>all</b> objects in a single transaction.
557
573
*/
558
574
public void removeAll () {
559
575
Cursor <T > cursor = getWriter ();
0 commit comments