Skip to content

Commit 6c8f6e5

Browse files
Prepare ToMany and RelationInfo for backlinks from ToMany.
1 parent aaa1012 commit 6c8f6e5

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,30 @@
2626
import io.objectbox.internal.ToManyGetter;
2727
import io.objectbox.internal.ToOneGetter;
2828

29-
@Internal
30-
@Immutable
3129
/**
3230
* Meta info describing a relation including source and target entity.
3331
*/
32+
@Internal
33+
@Immutable
3434
public class RelationInfo<TARGET> implements Serializable {
3535
private static final long serialVersionUID = 7412962174183812632L;
3636

3737
public final EntityInfo sourceInfo;
3838
public final EntityInfo<TARGET> targetInfo;
3939

40-
/** For relations based on a target ID property (null for stand-alone relations). */
40+
/** For relations based on a target ID property (null otherwise). */
4141
public final Property targetIdProperty;
4242

43+
/** For ToMany relations based on ToMany backlinks (0 otherwise). */
44+
public final int targetRelationId;
45+
4346
/** Only set for ToOne relations */
4447
public final ToOneGetter toOneGetter;
4548

4649
/** Only set for ToMany relations */
4750
public final ToManyGetter toManyGetter;
4851

49-
/** For ToMany relations based on backlinks (null for stand-alone relations). */
52+
/** For ToMany relations based on ToOne backlinks (null otherwise). */
5053
public final ToOneGetter backlinkToOneGetter;
5154

5255
/** For stand-alone to-many relations (0 otherwise). */
@@ -61,6 +64,7 @@ public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, Proper
6164
this.targetInfo = targetInfo;
6265
this.targetIdProperty = targetIdProperty;
6366
this.toOneGetter = toOneGetter;
67+
this.targetRelationId = 0;
6468
this.backlinkToOneGetter = null;
6569
this.toManyGetter = null;
6670
this.relationId = 0;
@@ -76,22 +80,38 @@ public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, ToMany
7680
this.targetIdProperty = targetIdProperty;
7781
this.toManyGetter = toManyGetter;
7882
this.backlinkToOneGetter = backlinkToOneGetter;
83+
this.targetRelationId = 0;
7984
this.toOneGetter = null;
8085
this.relationId = 0;
8186
}
8287

8388
/**
84-
* Stand-alone ToMany.
89+
* ToMany as a ToMany backlink or stand-alone ToMany.
8590
*/
8691
public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter toManyGetter,
87-
int relationId) {
92+
int relationId, boolean isBacklink) {
8893
this.sourceInfo = sourceInfo;
8994
this.targetInfo = targetInfo;
90-
this.relationId = relationId;
9195
this.toManyGetter = toManyGetter;
9296
this.targetIdProperty = null;
9397
this.toOneGetter = null;
9498
this.backlinkToOneGetter = null;
99+
if (isBacklink) {
100+
this.relationId = 0;
101+
this.targetRelationId = relationId;
102+
} else {
103+
// stand-alone
104+
this.relationId = relationId;
105+
this.targetRelationId = 0;
106+
}
107+
}
108+
109+
/**
110+
* Stand-alone ToMany.
111+
*/
112+
public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter toManyGetter,
113+
int relationId) {
114+
this(sourceInfo, targetInfo, toManyGetter, relationId, false);
95115
}
96116

97117
@Override

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,15 @@ private void ensureEntities() {
189189
int sourceEntityId = relationInfo.sourceInfo.getEntityId();
190190
newEntities = targetBox.internalGetRelationEntities(sourceEntityId, relationId, id, false);
191191
} else {
192-
newEntities = targetBox.internalGetBacklinkEntities(relationInfo.targetInfo.getEntityId(),
193-
relationInfo.targetIdProperty, id);
192+
if (relationInfo.targetIdProperty != null) {
193+
// Backlink from ToOne
194+
newEntities = targetBox.internalGetBacklinkEntities(relationInfo.targetInfo.getEntityId(),
195+
relationInfo.targetIdProperty, id);
196+
} else {
197+
// Backlink from ToMany
198+
newEntities = targetBox.internalGetRelationEntities(relationInfo.targetInfo.getEntityId(),
199+
relationInfo.targetRelationId, id, true);
200+
}
194201
}
195202
if (comparator != null) {
196203
Collections.sort(newEntities, comparator);
@@ -670,6 +677,11 @@ public boolean internalCheckApplyToDbRequired() {
670677

671678
private boolean prepareBacklinkEntitiesForDb() {
672679
ToOneGetter backlinkToOneGetter = relationInfo.backlinkToOneGetter;
680+
if (backlinkToOneGetter == null) {
681+
// backlink from ToMany, do not apply changes to db
682+
return false;
683+
}
684+
673685
long entityId = relationInfo.sourceInfo.getIdGetter().getId(entity);
674686
if (entityId == 0) {
675687
throw new IllegalStateException("Source entity has no ID (should have been put before)");

0 commit comments

Comments
 (0)