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