2828import javax .annotation .Nullable ;
2929import javax .annotation .concurrent .ThreadSafe ;
3030
31+ import io .objectbox .annotation .Backlink ;
32+ import io .objectbox .annotation .Id ;
3133import io .objectbox .annotation .apihint .Beta ;
3234import io .objectbox .annotation .apihint .Experimental ;
3335import io .objectbox .annotation .apihint .Internal ;
3840import io .objectbox .query .QueryBuilder ;
3941import io .objectbox .query .QueryCondition ;
4042import io .objectbox .relation .RelationInfo ;
43+ import io .objectbox .relation .ToMany ;
44+ import io .objectbox .relation .ToOne ;
4145
4246/**
4347 * A Box to put and get Objects of a specific Entity class.
@@ -335,12 +339,25 @@ public boolean contains(long id) {
335339 }
336340
337341 /**
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.
341343 * <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.
344361 */
345362 public long put (T entity ) {
346363 Cursor <T > cursor = getWriter ();
@@ -432,9 +449,11 @@ public void putBatched(@Nullable Collection<T> entities, int batchSize) {
432449 }
433450
434451 /**
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.
436455 *
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.
438457 */
439458 public boolean remove (long id ) {
440459 Cursor <T > cursor = getWriter ();
@@ -449,7 +468,7 @@ public boolean remove(long id) {
449468 }
450469
451470 /**
452- * Removes (deletes) Objects by their ID in a single transaction.
471+ * Like {@link #remove(long)}, but removes multiple objects in a single transaction.
453472 */
454473 public void remove (@ Nullable long ... ids ) {
455474 if (ids == null || ids .length == 0 ) {
@@ -476,7 +495,7 @@ public void removeByKeys(@Nullable Collection<Long> ids) {
476495 }
477496
478497 /**
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 .
480499 */
481500 public void removeByIds (@ Nullable Collection <Long > ids ) {
482501 if (ids == null || ids .isEmpty ()) {
@@ -494,9 +513,7 @@ public void removeByIds(@Nullable Collection<Long> ids) {
494513 }
495514
496515 /**
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.
500517 */
501518 public boolean remove (T object ) {
502519 Cursor <T > cursor = getWriter ();
@@ -512,7 +529,7 @@ public boolean remove(T object) {
512529 }
513530
514531 /**
515- * Removes (deletes) the given Objects in a single transaction.
532+ * Like {@link #remove(Object)}, but removes multiple objects in a single transaction.
516533 */
517534 @ SafeVarargs // Not using T... as Object[], no ClassCastException expected.
518535 @ SuppressWarnings ("Duplicates" ) // Detected duplicate has different type
@@ -533,7 +550,7 @@ public final void remove(@Nullable T... objects) {
533550 }
534551
535552 /**
536- * Removes (deletes) the given Objects in a single transaction.
553+ * Like {@link #remove(Object)}, but removes multiple objects in a single transaction.
537554 */
538555 @ SuppressWarnings ("Duplicates" ) // Detected duplicate has different type
539556 public void remove (@ Nullable Collection <T > objects ) {
@@ -553,7 +570,7 @@ public void remove(@Nullable Collection<T> objects) {
553570 }
554571
555572 /**
556- * Removes (deletes) ALL Objects in a single transaction.
573+ * Like {@link #remove(long)}, but removes <b>all</b> objects in a single transaction.
557574 */
558575 public void removeAll () {
559576 Cursor <T > cursor = getWriter ();
@@ -578,9 +595,10 @@ public long panicModeRemoveAll() {
578595
579596 /**
580597 * 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.
583600 */
601+ @ Deprecated
584602 public QueryBuilder <T > query () {
585603 return new QueryBuilder <>(this , store .getNativeStore (), store .getDbName (entityClass ));
586604 }
0 commit comments