Skip to content

Commit 2d48928

Browse files
committed
added QueryBuilder.backlink
1 parent a74d721 commit 2d48928

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

objectbox-java/src/main/java/io/objectbox/query/QueryBuilder.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323

2424
import io.objectbox.Box;
25+
import io.objectbox.EntityInfo;
2526
import io.objectbox.Property;
2627
import io.objectbox.annotation.apihint.Experimental;
2728
import io.objectbox.annotation.apihint.Internal;
@@ -285,14 +286,38 @@ public QueryBuilder<T> sort(Comparator<T> comparator) {
285286
* @return A builder to define query conditions at the target entity side.
286287
*/
287288
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) {
290294
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,
292296
relationInfo.relationId, relationInfo.isBacklink());
293297
return new QueryBuilder<>(storeHandle, linkQBHandle);
294298
}
295299

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+
296321
/**
297322
* Specifies relations that should be resolved eagerly.
298323
* This prepares the given relation objects to be preloaded (cached) avoiding further get operations from the db.

0 commit comments

Comments
 (0)