|
1 | 1 | /*
|
2 |
| - * Copyright 2017 ObjectBox Ltd. All rights reserved. |
| 2 | + * Copyright 2017-2024 ObjectBox Ltd. All rights reserved. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
36 | 36 | import io.objectbox.Cursor;
|
37 | 37 | import io.objectbox.InternalAccess;
|
38 | 38 | import io.objectbox.annotation.Backlink;
|
| 39 | +import io.objectbox.annotation.Entity; |
39 | 40 | import io.objectbox.annotation.apihint.Beta;
|
40 | 41 | import io.objectbox.annotation.apihint.Experimental;
|
41 | 42 | import io.objectbox.annotation.apihint.Internal;
|
|
51 | 52 | import static java.lang.Boolean.TRUE;
|
52 | 53 |
|
53 | 54 | /**
|
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. |
56 | 56 | * <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> |
60 | 71 | * <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. |
65 | 74 | * <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. |
66 | 77 | * It is possible to preload the list when running a query using {@link QueryBuilder#eager}.
|
67 | 78 | * <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> |
68 | 104 | * 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}. |
69 | 107 | *
|
70 |
| - * @param <TARGET> Object type (entity). |
| 108 | + * @param <TARGET> target object type ({@link Entity @Entity} class). |
71 | 109 | */
|
72 | 110 | public class ToMany<TARGET> implements List<TARGET>, Serializable {
|
73 | 111 | private static final long serialVersionUID = 2367317778240689006L;
|
|
0 commit comments