Skip to content

Commit 6b8ba61

Browse files
External type: also support, smoke test for standalone ToMany objectbox-java#239
1 parent b4eee52 commit 6b8ba61

File tree

7 files changed

+93
-6
lines changed

7 files changed

+93
-6
lines changed

objectbox-java-api/src/main/java/io/objectbox/annotation/ExternalType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
/**
26-
* Use to set the type of property in an external system (like another database).
26+
* Sets the type of a property or the type of object IDs of a ToMany in an external system (like another database).
2727
* <p>
2828
* This is useful if there is no default mapping of the ObjectBox type to the type in the external system.
2929
* <p>

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public PropertyBuilder indexMaxValueLength(int indexMaxValueLength) {
149149
}
150150

151151
/**
152-
* Set a {@link ExternalPropertyType} constant.
152+
* Sets the {@link ExternalPropertyType} constant for this.
153153
*
154154
* @return this builder.
155155
*/
@@ -261,6 +261,7 @@ public static class RelationBuilder extends PartBuilder {
261261
private final long relationUid;
262262
private final int targetEntityId;
263263
private final long targetEntityUid;
264+
private int externalPropertyType;
264265

265266
private RelationBuilder(FlatBufferBuilder fbb, String name, int relationId, long relationUid,
266267
int targetEntityId, long targetEntityUid) {
@@ -272,6 +273,17 @@ private RelationBuilder(FlatBufferBuilder fbb, String name, int relationId, long
272273
this.targetEntityUid = targetEntityUid;
273274
}
274275

276+
/**
277+
* Sets the {@link ExternalPropertyType} constant for this.
278+
*
279+
* @return this builder.
280+
*/
281+
public RelationBuilder externalType(int externalPropertyType) {
282+
checkNotFinished();
283+
this.externalPropertyType = externalPropertyType;
284+
return this;
285+
}
286+
275287
@Override
276288
public int createFlatBufferTable(FlatBufferBuilder fbb) {
277289
int nameOffset = fbb.createString(name);
@@ -282,6 +294,9 @@ public int createFlatBufferTable(FlatBufferBuilder fbb) {
282294
ModelRelation.addId(fbb, relationIdOffset);
283295
int targetEntityIdOffset = IdUid.createIdUid(fbb, targetEntityId, targetEntityUid);
284296
ModelRelation.addTargetEntityId(fbb, targetEntityIdOffset);
297+
if (externalPropertyType != 0) {
298+
ModelRelation.addExternalType(fbb, externalPropertyType);
299+
}
285300
return ModelRelation.endModelRelation(fbb);
286301
}
287302
}

tests/objectbox-java-test/src/main/java/io/objectbox/relation/Customer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.objectbox.BoxStore;
2323
import io.objectbox.annotation.Backlink;
2424
import io.objectbox.annotation.Entity;
25+
import io.objectbox.annotation.ExternalPropertyType;
26+
import io.objectbox.annotation.ExternalType;
2527
import io.objectbox.annotation.Id;
2628
import io.objectbox.annotation.Index;
2729

@@ -51,6 +53,10 @@ public class Customer implements Serializable {
5153

5254
ToMany<Order> ordersStandalone = new ToMany<>(this, Customer_.ordersStandalone);
5355

56+
// Just smoke testing, also use UUID instead of the default Mongo ID
57+
@ExternalType(ExternalPropertyType.UUID_VECTOR)
58+
private ToMany<Order> toManyExternalId = new ToMany<>(this, Customer_.toManyExternalId);
59+
5460
// Note: in a typical project the BoxStore field is added by the ObjectBox byte code transformer
5561
// https://docs.objectbox.io/relations#initialization-magic
5662
transient BoxStore __boxStore;
@@ -86,4 +92,8 @@ public List<Order> getOrders() {
8692
public ToMany<Order> getOrdersStandalone() {
8793
return ordersStandalone;
8894
}
95+
96+
public ToMany<Order> getToManyExternalId() {
97+
return toManyExternalId;
98+
}
8999
}

tests/objectbox-java-test/src/main/java/io/objectbox/relation/CustomerCursor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public long put(Customer entity) {
7171

7272
checkApplyToManyToDb(entity.getOrders(), Order.class);
7373
checkApplyToManyToDb(entity.getOrdersStandalone(), Order.class);
74+
checkApplyToManyToDb(entity.getToManyExternalId(), Order.class);
7475

7576
return __assignedId;
7677
}

tests/objectbox-java-test/src/main/java/io/objectbox/relation/Customer_.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,14 @@ public List<Order> getToMany(Customer customer) {
129129
}
130130
}, 1);
131131

132+
/** To-many relation "toManyExternalId" to target entity "Order". */
133+
public static final RelationInfo<Customer, Order> toManyExternalId = new RelationInfo<>(Customer_.__INSTANCE, Order_.__INSTANCE,
134+
new ToManyGetter<Customer, Order>() {
135+
@Override
136+
public List<Order> getToMany(Customer entity) {
137+
return entity.getToManyExternalId();
138+
}
139+
},
140+
2);
141+
132142
}

tests/objectbox-java-test/src/main/java/io/objectbox/relation/MyObjectBox.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.objectbox.BoxStoreBuilder;
2121
import io.objectbox.ModelBuilder;
2222
import io.objectbox.ModelBuilder.EntityBuilder;
23+
import io.objectbox.model.ExternalPropertyType;
2324
import io.objectbox.model.PropertyFlags;
2425
import io.objectbox.model.PropertyType;
2526

@@ -45,17 +46,22 @@ private static byte[] getModel() {
4546
ModelBuilder modelBuilder = new ModelBuilder();
4647
modelBuilder.lastEntityId(4, 5318696586219463633L);
4748
modelBuilder.lastIndexId(2, 8919874872236271392L);
48-
modelBuilder.lastRelationId(1, 8943758920347589435L);
49+
modelBuilder.lastRelationId(2, 297832184913930702L);
4950

50-
EntityBuilder entityBuilder;
51-
52-
entityBuilder = modelBuilder.entity("Customer");
51+
EntityBuilder entityBuilder = modelBuilder.entity("Customer");
5352
entityBuilder.id(1, 8247662514375611729L).lastPropertyId(2, 7412962174183812632L);
5453
entityBuilder.property("_id", PropertyType.Long).id(1, 1888039726372206411L)
5554
.flags(PropertyFlags.ID | PropertyFlags.ID_SELF_ASSIGNABLE);
5655
entityBuilder.property("name", PropertyType.String).id(2, 7412962174183812632L)
5756
.flags(PropertyFlags.INDEXED).indexId(1, 5782921847050580892L);
57+
5858
entityBuilder.relation("ordersStandalone", 1, 8943758920347589435L, 3, 6367118380491771428L);
59+
60+
// Note: there is no way to test external type mapping works here. Instead, verify passing a model with
61+
// externalType(int) works.
62+
entityBuilder.relation("toManyExternalId", 2, 297832184913930702L, 3, 6367118380491771428L)
63+
.externalType(ExternalPropertyType.UuidVector);
64+
5965
entityBuilder.entityDone();
6066

6167

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.objectbox.relation;
18+
19+
20+
import org.junit.Test;
21+
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
public class ExternalTypeTest extends AbstractRelationTest {
26+
27+
/**
28+
* There is no way to test external type mapping works here. Instead, verify passing a model with
29+
* {@link io.objectbox.ModelBuilder.RelationBuilder#externalType(int)} works (see {@link MyObjectBox}) and that
30+
* there are no side effects for put and get.
31+
*/
32+
@Test
33+
public void standaloneToMany_externalType_putGetSmokeTest() {
34+
Customer putCustomer = new Customer();
35+
putCustomer.setName("Joe");
36+
Order order = new Order();
37+
order.setText("Order from Joe");
38+
putCustomer.getToManyExternalId().add(order);
39+
long customerId = customerBox.put(putCustomer);
40+
41+
Customer readCustomer = customerBox.get(customerId);
42+
assertEquals(order.getText(), readCustomer.getToManyExternalId().get(0).getText());
43+
}
44+
45+
}

0 commit comments

Comments
 (0)