Skip to content

Commit 9886e98

Browse files
ToOne/ToMany: improve class descriptions, notable things first
1 parent b79519d commit 9886e98

File tree

2 files changed

+101
-18
lines changed

2 files changed

+101
-18
lines changed

objectbox-java/src/main/java/io/objectbox/relation/ToMany.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
import io.objectbox.Cursor;
3737
import io.objectbox.InternalAccess;
3838
import io.objectbox.annotation.Backlink;
39+
import io.objectbox.annotation.Entity;
3940
import io.objectbox.annotation.apihint.Beta;
4041
import io.objectbox.annotation.apihint.Experimental;
4142
import io.objectbox.annotation.apihint.Internal;
@@ -51,23 +52,60 @@
5152
import static java.lang.Boolean.TRUE;
5253

5354
/**
54-
* A lazily loaded {@link List} of target objects representing a to-many relation, a unidirectional link from a "source"
55-
* entity to multiple objects of a "target" entity.
55+
* A to-many relation of an entity that references multiple objects of a {@link TARGET} entity.
5656
* <p>
57-
* It tracks changes (adds and removes) that can be later applied (persisted) to the database. This happens either when
58-
* the object that contains this relation is put or using {@link #applyChangesToDb()}. For some important details about
59-
* applying changes, see the notes about relations of {@link Box#put(Object)}.
57+
* Example:
58+
* <pre>{@code
59+
* // Java
60+
* @Entity
61+
* public class Student{
62+
* private ToMany<Teacher> teachers;
63+
* }
64+
*
65+
* // Kotlin
66+
* @Entity
67+
* data class Student() {
68+
* lateinit var teachers: ToMany<Teacher>
69+
* }
70+
* }</pre>
6071
* <p>
61-
* The objects are loaded lazily on first access of this list, and then cached. The database query runs on the calling
62-
* thread, so avoid accessing this from a UI or main thread. Subsequent calls to any method, like {@link #size()}, do
63-
* not query the database, even if the relation was changed elsewhere. To get the latest data {@link Box#get} the source
64-
* object again or use {@link #reset()} before accessing the list again.
72+
* Implements the {@link List} interface and uses lazy initialization. The target objects are only read from the
73+
* database when the list is first accessed.
6574
* <p>
75+
* The required database query runs on the calling thread, so avoid accessing ToMany from a UI or main thread. To get the
76+
* latest data {@link Box#get} the object with the ToMany again or use {@link #reset()} before accessing the list again.
6677
* It is possible to preload the list when running a query using {@link QueryBuilder#eager}.
6778
* <p>
79+
* Tracks when target objects are added and removed. Common usage:
80+
* <ul>
81+
* <li>{@link #add(Object)} to add target objects to the relation.
82+
* <li>{@link #remove(Object)} to remove target objects from the relation.
83+
* <li>{@link #remove(int)} to remove target objects at a specific index.
84+
* </ul>
85+
* <p>
86+
* To apply (persist) the changes to the database, call {@link #applyChangesToDb()} or put the object with the ToMany.
87+
* For important details, see the notes about relations of {@link Box#put(Object)}.
88+
* <p>
89+
* <pre>{@code
90+
* // Example 1: add target objects to a relation
91+
* student.getTeachers().add(teacher1);
92+
* student.getTeachers().add(teacher2);
93+
* store.boxFor(Student.class).put(student);
94+
*
95+
* // Example 2: remove a target object from the relation
96+
* student.getTeachers().remove(index);
97+
* student.getTeachers().applyChangesToDb();
98+
* // or store.boxFor(Student.class).put(student);
99+
* }</pre>
100+
* <p>
101+
* In the database, the target objects are referenced by their IDs, which are persisted as part of the relation of the
102+
* object with the ToMany.
103+
* <p>
68104
* ToMany is thread-safe by default (may not be the case if {@link #setListFactory(ListFactory)} is used).
105+
* <p>
106+
* To get all objects with a ToMany that reference a target object, see {@link Backlink}.
69107
*
70-
* @param <TARGET> Object type (entity).
108+
* @param <TARGET> target object type ({@link Entity @Entity} class).
71109
*/
72110
public class ToMany<TARGET> implements List<TARGET>, Serializable {
73111
private static final long serialVersionUID = 2367317778240689006L;

objectbox-java/src/main/java/io/objectbox/relation/ToOne.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,18 +24,63 @@
2424
import io.objectbox.Box;
2525
import io.objectbox.BoxStore;
2626
import io.objectbox.Cursor;
27+
import io.objectbox.annotation.Backlink;
28+
import io.objectbox.annotation.Entity;
2729
import io.objectbox.annotation.apihint.Internal;
2830
import io.objectbox.exception.DbDetachedException;
2931
import io.objectbox.internal.ReflectionCache;
3032

3133
/**
32-
* Manages a to-one relation: resolves the target object, keeps the target Id in sync, etc.
33-
* A to-relation is unidirectional: it points from the source entity to the target entity.
34-
* The target is referenced by its ID, which is persisted in the source entity.
34+
* A to-one relation of an entity that references one object of a {@link TARGET} entity.
3535
* <p>
36-
* If there is a {@link ToMany} relation linking back to this to-one relation (@Backlink),
37-
* the ToMany object will not be notified/updated about persisted changes here.
38-
* Call {@link ToMany#reset()} so it will update when next accessed.
36+
* Example:
37+
* <pre>{@code
38+
* // Java
39+
* @Entity
40+
* public class Order {
41+
* private ToOne<Customer> customer;
42+
* }
43+
*
44+
* // Kotlin
45+
* @Entity
46+
* data class Order() {
47+
* lateinit var customer: ToOne<Customer>
48+
* }
49+
* }</pre>
50+
* <p>
51+
* Uses lazy initialization. The target object ({@link #getTarget()}) is only read from the database when it is first
52+
* accessed.
53+
* <p>
54+
* Common usage:
55+
* <ul>
56+
* <li>Set the target object with {@link #setTarget} to create a relation.
57+
* When the object with the ToOne is put, if the target object is new (its ID is 0), it will be put as well.
58+
* Otherwise, only the target ID in the database is updated.
59+
* <li>{@link #setTargetId} of the target object to create a relation.
60+
* <li>{@link #setTarget} with {@code null} or {@link #setTargetId} to {@code 0} to remove the relation.
61+
* </ul>
62+
* <p>
63+
* Then, to persist the changes {@link Box#put} the object with the ToOne.
64+
* <p>
65+
* <pre>{@code
66+
* // Example 1: create a relation
67+
* order.getCustomer().setTarget(customer);
68+
* // or order.getCustomer().setTargetId(customerId);
69+
* store.boxFor(Order.class).put(order);
70+
*
71+
* // Example 2: remove the relation
72+
* order.getCustomer().setTarget(null);
73+
* // or order.getCustomer().setTargetId(0);
74+
* store.boxFor(Order.class).put(order);
75+
* }</pre>
76+
* <p>
77+
* The target object is referenced by its ID.
78+
* This target ID ({@link #getTargetId()}) is persisted as part of the object with the ToOne in a special
79+
* property created for each ToOne (named like "customerId").
80+
* <p>
81+
* To get all objects with a ToOne that reference a target object, see {@link Backlink}.
82+
*
83+
* @param <TARGET> target object type ({@link Entity @Entity} class).
3984
*/
4085
// TODO not exactly thread safe
4186
public class ToOne<TARGET> implements Serializable {

0 commit comments

Comments
 (0)