Skip to content

Commit c654aae

Browse files
Merge branch 'docs-updates' into 'dev'
Docs: details for put and remove, more ToMany details See merge request objectbox/objectbox-java!134
2 parents 2b187ff + e89779a commit c654aae

File tree

4 files changed

+125
-53
lines changed

4 files changed

+125
-53
lines changed

objectbox-java/src/main/java/io/objectbox/Box.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import javax.annotation.Nullable;
2929
import javax.annotation.concurrent.ThreadSafe;
3030

31+
import io.objectbox.annotation.Backlink;
32+
import io.objectbox.annotation.Id;
3133
import io.objectbox.annotation.apihint.Beta;
3234
import io.objectbox.annotation.apihint.Experimental;
3335
import io.objectbox.annotation.apihint.Internal;
@@ -38,6 +40,8 @@
3840
import io.objectbox.query.QueryBuilder;
3941
import io.objectbox.query.QueryCondition;
4042
import 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
}

objectbox-java/src/main/java/io/objectbox/query/Query.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
import io.objectbox.BoxStore;
3232
import io.objectbox.InternalAccess;
3333
import io.objectbox.Property;
34+
import io.objectbox.exception.NonUniqueResultException;
3435
import io.objectbox.reactive.DataObserver;
3536
import io.objectbox.reactive.DataSubscriptionList;
3637
import io.objectbox.reactive.SubscriptionBuilder;
3738
import io.objectbox.relation.RelationInfo;
3839
import io.objectbox.relation.ToOne;
3940

4041
/**
41-
* A repeatable Query returning the latest matching Objects.
42+
* A repeatable Query returning the latest matching objects.
4243
* <p>
43-
* Use {@link #find()} or related methods to fetch the latest results from the BoxStore.
44+
* Use {@link #find()} or related methods to fetch the latest results from the {@link BoxStore}.
4445
* <p>
4546
* Use {@link #property(Property)} to only return values or an aggregate of a single Property.
4647
* <p>
@@ -195,7 +196,12 @@ long cursorHandle() {
195196
}
196197

197198
/**
198-
* Find the first Object matching the query.
199+
* Finds the first object matching this query.
200+
* <p>
201+
* Note: if no {@link QueryBuilder#order} conditions are present, which object is the first one might be arbitrary
202+
* (sometimes the one with the lowest ID, but never guaranteed to be).
203+
*
204+
* @return The first object if there are matches. {@code null} if no object matches.
199205
*/
200206
@Nullable
201207
public T findFirst() {
@@ -228,9 +234,10 @@ private void ensureNoComparator() {
228234
}
229235

230236
/**
231-
* If there is a single matching object, returns it. If there is more than one matching object,
232-
* throws {@link io.objectbox.exception.NonUniqueResultException NonUniqueResultException}.
233-
* If there are no matches returns null.
237+
* Finds the only object matching this query.
238+
*
239+
* @return The object if a single object matches. {@code null} if no object matches. Throws
240+
* {@link NonUniqueResultException} if there are multiple objects matching the query.
234241
*/
235242
@Nullable
236243
public T findUnique() {
@@ -244,7 +251,12 @@ public T findUnique() {
244251
}
245252

246253
/**
247-
* Find all Objects matching the query.
254+
* Finds objects matching the query.
255+
* <p>
256+
* Note: if no {@link QueryBuilder#order} conditions are present, the order is arbitrary (sometimes ordered by ID,
257+
* but never guaranteed to).
258+
*
259+
* @return A list of matching objects. An empty list if no object matches.
248260
*/
249261
@Nonnull
250262
public List<T> find() {
@@ -268,8 +280,12 @@ public List<T> find() {
268280
}
269281

270282
/**
271-
* Find all Objects matching the query, skipping the first offset results and returning at most limit results.
272-
* Use this for pagination.
283+
* Like {@link #find()}, but can skip and limit results.
284+
* <p>
285+
* Use to get a slice of the whole result, e.g. for "result paging".
286+
*
287+
* @param offset If greater than 0, skips this many results.
288+
* @param limit If greater than 0, returns at most this many results.
273289
*/
274290
@Nonnull
275291
public List<T> find(final long offset, final long limit) {
@@ -282,46 +298,59 @@ public List<T> find(final long offset, final long limit) {
282298
}
283299

284300
/**
285-
* Returns the ID of the first matching object. If there are no results returns 0.
301+
* Like {@link #findFirst()}, but returns just the ID of the object.
286302
* <p>
287-
* Like {@link #findFirst()}, but more efficient as no object is created.
303+
* This is more efficient as no object is created.
288304
* <p>
289305
* Ignores any {@link QueryBuilder#filter(QueryFilter) query filter}.
306+
*
307+
* @return The ID of the first matching object. {@code 0} if no object matches.
290308
*/
291309
public long findFirstId() {
292310
checkOpen();
293311
return box.internalCallWithReaderHandle(cursorHandle -> nativeFindFirstId(handle, cursorHandle));
294312
}
295313

296314
/**
297-
* If there is a single matching object, returns its ID. If there is more than one matching object,
298-
* throws {@link io.objectbox.exception.NonUniqueResultException NonUniqueResultException}.
299-
* If there are no matches returns 0.
315+
* Like {@link #findUnique()}, but returns just the ID of the object.
300316
* <p>
301-
* Like {@link #findUnique()}, but more efficient as no object is created.
317+
* This is more efficient as no object is created.
302318
* <p>
303319
* Ignores any {@link QueryBuilder#filter(QueryFilter) query filter}.
320+
*
321+
* @return The ID of the object, if a single object matches. {@code 0} if no object matches. Throws
322+
* {@link NonUniqueResultException} if there are multiple objects matching the query.
304323
*/
305324
public long findUniqueId() {
306325
checkOpen();
307326
return box.internalCallWithReaderHandle(cursorHandle -> nativeFindUniqueId(handle, cursorHandle));
308327
}
309328

310329
/**
311-
* Very efficient way to get just the IDs without creating any objects. IDs can later be used to lookup objects
312-
* (lookups by ID are also very efficient in ObjectBox).
330+
* Like {@link #find()}, but returns just the IDs of the objects.
331+
* <p>
332+
* IDs can later be used to {@link Box#get} objects.
333+
* <p>
334+
* This is very efficient as no objects are created.
313335
* <p>
314336
* Note: a filter set with {@link QueryBuilder#filter(QueryFilter)} will be silently ignored!
337+
*
338+
* @return An array of IDs of matching objects. An empty array if no objects match.
315339
*/
316340
@Nonnull
317341
public long[] findIds() {
318342
return findIds(0, 0);
319343
}
320344

321345
/**
322-
* Like {@link #findIds()} but with a offset/limit param, e.g. for pagination.
346+
* Like {@link #findIds()}, but can skip and limit results.
347+
* <p>
348+
* Use to get a slice of the whole result, e.g. for "result paging".
323349
* <p>
324350
* Note: a filter set with {@link QueryBuilder#filter(QueryFilter)} will be silently ignored!
351+
*
352+
* @param offset If greater than 0, skips this many results.
353+
* @param limit If greater than 0, returns at most this many results.
325354
*/
326355
@Nonnull
327356
public long[] findIds(final long offset, final long limit) {

objectbox-java/src/main/java/io/objectbox/query/QueryBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ public <TARGET> QueryBuilder<TARGET> backlink(RelationInfo<TARGET, ?> relationIn
425425

426426
/**
427427
* Specifies relations that should be resolved eagerly.
428-
* This prepares the given relation objects to be preloaded (cached) avoiding further get operations from the db.
429-
* A common use case is prealoading all
428+
* This prepares the given relation objects to be preloaded (cached) avoiding further get operations from the database.
430429
*
431430
* @param relationInfo The relation as found in the generated meta info class ("EntityName_") of class T.
432431
* @param more Supply further relations to be eagerly loaded.

0 commit comments

Comments
 (0)