|
22 | 22 | import java.util.List;
|
23 | 23 |
|
24 | 24 | import io.objectbox.Box;
|
| 25 | +import io.objectbox.EntityInfo; |
25 | 26 | import io.objectbox.Property;
|
26 | 27 | import io.objectbox.annotation.apihint.Experimental;
|
27 | 28 | import io.objectbox.annotation.apihint.Internal;
|
@@ -285,14 +286,38 @@ public QueryBuilder<T> sort(Comparator<T> comparator) {
|
285 | 286 | * @return A builder to define query conditions at the target entity side.
|
286 | 287 | */
|
287 | 288 | public <TARGET> QueryBuilder<TARGET> link(RelationInfo<TARGET> relationInfo) {
|
288 |
| - int sourceEntityId = relationInfo.sourceInfo.getEntityId(); |
289 |
| - int targetEntityId = relationInfo.targetInfo.getEntityId(); |
| 289 | + return link(relationInfo, relationInfo.sourceInfo, relationInfo.targetInfo, relationInfo.isBacklink()); |
| 290 | + } |
| 291 | + |
| 292 | + private <TARGET> QueryBuilder<TARGET> link(RelationInfo relationInfo, EntityInfo source, EntityInfo target, |
| 293 | + boolean backlink) { |
290 | 294 | int propertyId = relationInfo.targetIdProperty != null ? relationInfo.targetIdProperty.id : 0;
|
291 |
| - long linkQBHandle = nativeLink(handle, storeHandle, sourceEntityId, targetEntityId, propertyId, |
| 295 | + long linkQBHandle = nativeLink(handle, storeHandle, source.getEntityId(), target.getEntityId(), propertyId, |
292 | 296 | relationInfo.relationId, relationInfo.isBacklink());
|
293 | 297 | return new QueryBuilder<>(storeHandle, linkQBHandle);
|
294 | 298 | }
|
295 | 299 |
|
| 300 | + /** |
| 301 | + * Creates a backlink (reversed link) to another entity, |
| 302 | + * for which you also can describe conditions using the returned builder. |
| 303 | + * <p> |
| 304 | + * Note: only use this method over {@link #link(RelationInfo)}, |
| 305 | + * if you did not define @{@link io.objectbox.annotation.Backlink} in the entity already. |
| 306 | + * <p> |
| 307 | + * Note: in relational databases you would use a "join" for this. |
| 308 | + * |
| 309 | + * @param relationInfo Relation meta info (generated) of the original relation (reverse direction) |
| 310 | + * @param <TARGET> The target entity. For parent/tree like relations, it can be the same type. |
| 311 | + * @return A builder to define query conditions at the target entity side. |
| 312 | + */ |
| 313 | + public <TARGET> QueryBuilder<TARGET> backlink(RelationInfo relationInfo) { |
| 314 | + if (relationInfo.isBacklink()) { |
| 315 | + throw new IllegalArgumentException("Double backlink: The relation is already a backlink, please use a regular link on the original relation instead."); |
| 316 | + } |
| 317 | + |
| 318 | + return link(relationInfo, relationInfo.targetInfo, relationInfo.sourceInfo, false); |
| 319 | + } |
| 320 | + |
296 | 321 | /**
|
297 | 322 | * Specifies relations that should be resolved eagerly.
|
298 | 323 | * This prepares the given relation objects to be preloaded (cached) avoiding further get operations from the db.
|
|
0 commit comments