Skip to content

Commit 570760e

Browse files
Merge branch 'property-virtual' into dev
2 parents 6dc5d15 + b460b54 commit 570760e

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

objectbox-java/src/main/java/io/objectbox/Property.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class Property<ENTITY> implements Serializable {
4444

4545
public final String name;
4646
public final boolean isId;
47+
public final boolean isVirtual;
4748
public final String dbName;
4849
public final Class<? extends PropertyConverter> converterClass;
4950

@@ -58,6 +59,10 @@ public Property(EntityInfo<ENTITY> entity, int ordinal, int id, Class<?> type, S
5859
this(entity, ordinal, id, type, name, false, name, null, null);
5960
}
6061

62+
public Property(EntityInfo<ENTITY> entity, int ordinal, int id, Class<?> type, String name, boolean isVirtual) {
63+
this(entity, ordinal, id, type, name, false, isVirtual, name, null, null);
64+
}
65+
6166
public Property(EntityInfo<ENTITY> entity, int ordinal, int id, Class<?> type, String name, boolean isId,
6267
@Nullable String dbName) {
6368
this(entity, ordinal, id, type, name, isId, dbName, null, null);
@@ -66,12 +71,19 @@ public Property(EntityInfo<ENTITY> entity, int ordinal, int id, Class<?> type, S
6671
public Property(EntityInfo<ENTITY> entity, int ordinal, int id, Class<?> type, String name, boolean isId,
6772
@Nullable String dbName, @Nullable Class<? extends PropertyConverter> converterClass,
6873
@Nullable Class customType) {
74+
this(entity, ordinal, id, type, name, isId, false, dbName, converterClass, customType);
75+
}
76+
77+
public Property(EntityInfo<ENTITY> entity, int ordinal, int id, Class<?> type, String name, boolean isId,
78+
boolean isVirtual, @Nullable String dbName,
79+
@Nullable Class<? extends PropertyConverter> converterClass, @Nullable Class customType) {
6980
this.entity = entity;
7081
this.ordinal = ordinal;
7182
this.id = id;
7283
this.type = type;
7384
this.name = name;
7485
this.isId = isId;
86+
this.isVirtual = isVirtual;
7587
this.dbName = dbName;
7688
this.converterClass = converterClass;
7789
this.customType = customType;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public ToOne(Object sourceEntity, RelationInfo relationInfo) {
7979
}
8080
this.entity = sourceEntity;
8181
this.relationInfo = relationInfo;
82-
virtualProperty = relationInfo.targetIdProperty == null;
82+
virtualProperty = relationInfo.targetIdProperty.isVirtual;
8383
}
8484

8585
/**

tests/objectbox-java-test/src/test/java/io/objectbox/relation/ToOneTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class ToOneTest extends AbstractRelationTest {
3636

3737
@Test
38-
public void testTargetId_withTargetIdProperty() {
38+
public void testTargetId_regularTargetIdProperty() {
3939
Order entity = putOrder(null, null);
4040
ToOne<Customer> toOne = new ToOne<>(entity, getRelationInfo(Order_.customerId));
4141
entity.setCustomerId(1042);
@@ -49,10 +49,15 @@ private RelationInfo<Order, Customer> getRelationInfo(Property targetIdProperty)
4949
return new RelationInfo<>(new Order_(), new Customer_(), targetIdProperty, null);
5050
}
5151

52+
private RelationInfo<Order, Customer> getRelationInfoVirtualTargetProperty() {
53+
Property<Order> virtualTargetProperty = new Property<>(Order_.__INSTANCE, 2, 3, long.class, "customerId", true);
54+
return new RelationInfo<>(new Order_(), new Customer_(), virtualTargetProperty, null);
55+
}
56+
5257
@Test
53-
public void testTargetId_noTargetIdProperty() {
58+
public void testTargetId_virtualTargetIdProperty() {
5459
Order entity = putOrder(null, null);
55-
ToOne<Customer> toOne = new ToOne<>(entity, getRelationInfo(null));
60+
ToOne<Customer> toOne = new ToOne<>(entity, getRelationInfoVirtualTargetProperty());
5661
entity.setCustomerId(1042);
5762
assertEquals(0, toOne.getTargetId());
5863
toOne.setTargetId(1977);
@@ -71,15 +76,15 @@ public void testGetAndSetTarget() {
7176
customerBox.put(target, target2);
7277
Order source = putOrder(null, null);
7378

74-
// Without customerId
75-
ToOne<Customer> toOne = new ToOne<>(source, getRelationInfo(null));
79+
// With virtual customerId
80+
ToOne<Customer> toOne = new ToOne<>(source, getRelationInfoVirtualTargetProperty());
7681
toOne.setTargetId(1977);
7782
assertEquals("target1", toOne.getTarget().getName());
7883

7984
toOne.setTarget(target2);
8085
assertEquals(target2.getId(), toOne.getTargetId());
8186

82-
// With customerId
87+
// With regular customerId
8388
toOne = new ToOne<>(source, getRelationInfo(Order_.customerId));
8489
source.setCustomerId(1977);
8590
assertEquals("target1", toOne.getTarget().getName());

0 commit comments

Comments
 (0)