Skip to content

Commit 002527f

Browse files
Merge branch 'to-one-docs-polish' into 'dev'
ToOne: update setTarget and related docs to match implementation See merge request objectbox/objectbox-java!26
2 parents cfff985 + eee10d9 commit 002527f

File tree

1 file changed

+28
-11
lines changed
  • objectbox-java/src/main/java/io/objectbox/relation

1 file changed

+28
-11
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
* A to-relation is unidirectional: it points from the source entity to the target entity.
3434
* The target is referenced by its ID, which is persisted in the source entity.
3535
* <p>
36-
* If their is a backlink {@link ToMany} relation based on this to-one relation,
37-
* the ToMany object will not be notified/updated about changes done here (use {@link ToMany#reset()} if required).
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.
3839
*/
3940
// TODO add more tests
4041
// TODO not exactly thread safe
@@ -106,7 +107,7 @@ public TARGET getTarget(long targetId) {
106107
return targetNew;
107108
}
108109

109-
private void ensureBoxes(TARGET target) {
110+
private void ensureBoxes(@Nullable TARGET target) {
110111
// Only check the property set last
111112
if (targetBox == null) {
112113
Field boxStoreField = ReflectionCache.getInstance().getField(entity.getClass(), "__boxStore");
@@ -148,6 +149,14 @@ public boolean isNull() {
148149
return getTargetId() == 0 && target == null;
149150
}
150151

152+
/**
153+
* Sets or clears the target ID in the source entity. Pass 0 to clear.
154+
* <p>
155+
* Put the source entity to persist changes.
156+
* If the ID is not 0 creates a relation to the target entity with this ID, otherwise dissolves it.
157+
*
158+
* @see #setTarget
159+
*/
151160
public void setTargetId(long targetId) {
152161
if (virtualProperty) {
153162
this.targetId = targetId;
@@ -172,10 +181,13 @@ void setAndUpdateTargetId(long targetId) {
172181
}
173182

174183
/**
175-
* Sets the relation ID in the enclosed entity to the ID of the given target entity.
176-
* If the target entity was not put in the DB yet (its ID is 0), it will be put before to get its ID.
184+
* Sets or clears the target entity and ID in the source entity. Pass null to clear.
185+
* <p>
186+
* Put the source entity to persist changes.
187+
* If the target entity was not put yet (its ID is 0), it will be stored when the source entity is put.
188+
*
189+
* @see #setTargetId
177190
*/
178-
// TODO provide a overload with a ToMany parameter, which also gets updated
179191
public void setTarget(@Nullable final TARGET target) {
180192
if (target != null) {
181193
long targetId = relationInfo.targetInfo.getIdGetter().getId(target);
@@ -189,10 +201,11 @@ public void setTarget(@Nullable final TARGET target) {
189201
}
190202

191203
/**
192-
* Sets the relation ID in the enclosed entity to the ID of the given target entity and puts the enclosed entity.
193-
* If the target entity was not put in the DB yet (its ID is 0), it will be put before to get its ID.
204+
* Sets or clears the target entity and ID in the source entity, then puts the source entity to persist changes.
205+
* Pass null to clear.
206+
* <p>
207+
* If the target entity was not put yet (its ID is 0), it will be put before the source entity.
194208
*/
195-
// TODO provide a overload with a ToMany parameter, which also gets updated
196209
public void setAndPutTarget(@Nullable final TARGET target) {
197210
ensureBoxes(target);
198211
if (target != null) {
@@ -212,9 +225,13 @@ public void setAndPutTarget(@Nullable final TARGET target) {
212225
}
213226

214227
/**
215-
* Sets the relation ID in the enclosed entity to the ID of the given target entity and puts both entities.
228+
* Sets or clears the target entity and ID in the source entity,
229+
* then puts the target (if not null) and source entity to persist changes.
230+
* Pass null to clear.
231+
* <p>
232+
* When clearing the target entity, this does not remove it from its box.
233+
* This only dissolves the relation.
216234
*/
217-
// TODO provide a overload with a ToMany parameter, which also gets updated
218235
public void setAndPutTargetAlways(@Nullable final TARGET target) {
219236
ensureBoxes(target);
220237
if (target != null) {

0 commit comments

Comments
 (0)