33
33
* A to-relation is unidirectional: it points from the source entity to the target entity.
34
34
* The target is referenced by its ID, which is persisted in the source entity.
35
35
* <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.
38
39
*/
39
40
// TODO add more tests
40
41
// TODO not exactly thread safe
@@ -106,7 +107,7 @@ public TARGET getTarget(long targetId) {
106
107
return targetNew ;
107
108
}
108
109
109
- private void ensureBoxes (TARGET target ) {
110
+ private void ensureBoxes (@ Nullable TARGET target ) {
110
111
// Only check the property set last
111
112
if (targetBox == null ) {
112
113
Field boxStoreField = ReflectionCache .getInstance ().getField (entity .getClass (), "__boxStore" );
@@ -148,6 +149,14 @@ public boolean isNull() {
148
149
return getTargetId () == 0 && target == null ;
149
150
}
150
151
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
+ */
151
160
public void setTargetId (long targetId ) {
152
161
if (virtualProperty ) {
153
162
this .targetId = targetId ;
@@ -172,10 +181,13 @@ void setAndUpdateTargetId(long targetId) {
172
181
}
173
182
174
183
/**
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
177
190
*/
178
- // TODO provide a overload with a ToMany parameter, which also gets updated
179
191
public void setTarget (@ Nullable final TARGET target ) {
180
192
if (target != null ) {
181
193
long targetId = relationInfo .targetInfo .getIdGetter ().getId (target );
@@ -189,10 +201,11 @@ public void setTarget(@Nullable final TARGET target) {
189
201
}
190
202
191
203
/**
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.
194
208
*/
195
- // TODO provide a overload with a ToMany parameter, which also gets updated
196
209
public void setAndPutTarget (@ Nullable final TARGET target ) {
197
210
ensureBoxes (target );
198
211
if (target != null ) {
@@ -212,9 +225,13 @@ public void setAndPutTarget(@Nullable final TARGET target) {
212
225
}
213
226
214
227
/**
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.
216
234
*/
217
- // TODO provide a overload with a ToMany parameter, which also gets updated
218
235
public void setAndPutTargetAlways (@ Nullable final TARGET target ) {
219
236
ensureBoxes (target );
220
237
if (target != null ) {
0 commit comments