Skip to content

Commit 7b13561

Browse files
External name: add annotation, model API #239
1 parent bb45440 commit 7b13561

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2025 ObjectBox Ltd.
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.annotation;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Sets the name of an {@link Entity @Entity}, a property or a ToMany in an external system (like another database).
26+
*/
27+
@Retention(RetentionPolicy.CLASS)
28+
@Target({ElementType.TYPE, ElementType.FIELD})
29+
public @interface ExternalName {
30+
31+
/**
32+
* The name assigned to the annotated element in the external system.
33+
*/
34+
String value();
35+
36+
}

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import javax.annotation.Nullable;
2323

24+
import io.objectbox.annotation.ExternalName;
25+
import io.objectbox.annotation.ExternalType;
2426
import io.objectbox.annotation.HnswIndex;
2527
import io.objectbox.annotation.apihint.Internal;
2628
import io.objectbox.flatbuffers.FlatBufferBuilder;
@@ -123,6 +125,7 @@ public static class PropertyBuilder extends PartBuilder {
123125
private int indexId;
124126
private long indexUid;
125127
private int indexMaxValueLength;
128+
private int externalNameOffset;
126129
private int externalType;
127130
private int hnswParamsOffset;
128131
private int flags;
@@ -166,9 +169,16 @@ public PropertyBuilder indexMaxValueLength(int indexMaxValueLength) {
166169
}
167170

168171
/**
169-
* Sets the {@link ExternalPropertyType} constant for this.
170-
*
171-
* @return this builder.
172+
* Sets the {@link ExternalName} of this property.
173+
*/
174+
public PropertyBuilder externalName(String externalName) {
175+
checkNotFinished();
176+
externalNameOffset = getFbb().createString(externalName);
177+
return this;
178+
}
179+
180+
/**
181+
* Sets the {@link ExternalType} of this property. Should be one of {@link ExternalPropertyType}.
172182
*/
173183
public PropertyBuilder externalType(int externalType) {
174184
checkNotFinished();
@@ -247,6 +257,7 @@ public int createFlatBufferTable(FlatBufferBuilder fbb) {
247257
ModelProperty.addIndexId(fbb, indexIdOffset);
248258
}
249259
if (indexMaxValueLength > 0) ModelProperty.addMaxIndexValueLength(fbb, indexMaxValueLength);
260+
if (externalNameOffset != 0) ModelProperty.addExternalName(fbb, externalNameOffset);
250261
if (externalType != 0) ModelProperty.addExternalType(fbb, externalType);
251262
if (hnswParamsOffset != 0) ModelProperty.addHnswParams(fbb, hnswParamsOffset);
252263
if (flags != 0) ModelProperty.addFlags(fbb, flags);
@@ -262,6 +273,7 @@ public static class RelationBuilder extends PartBuilder {
262273
private final int targetEntityId;
263274
private final long targetEntityUid;
264275

276+
private int externalNameOffset;
265277
private int externalType;
266278

267279
private RelationBuilder(FlatBufferBuilder fbb, String name, int relationId, long relationUid,
@@ -275,9 +287,16 @@ private RelationBuilder(FlatBufferBuilder fbb, String name, int relationId, long
275287
}
276288

277289
/**
278-
* Sets the {@link ExternalPropertyType} constant for this.
279-
*
280-
* @return this builder.
290+
* Sets the {@link ExternalName} of this relation.
291+
*/
292+
public RelationBuilder externalName(String externalName) {
293+
checkNotFinished();
294+
externalNameOffset = getFbb().createString(externalName);
295+
return this;
296+
}
297+
298+
/**
299+
* Sets the {@link ExternalType} of this relation. Should be one of {@link ExternalPropertyType}.
281300
*/
282301
public RelationBuilder externalType(int externalType) {
283302
checkNotFinished();
@@ -295,6 +314,7 @@ public int createFlatBufferTable(FlatBufferBuilder fbb) {
295314
ModelRelation.addId(fbb, relationIdOffset);
296315
int targetEntityIdOffset = IdUid.createIdUid(fbb, targetEntityId, targetEntityUid);
297316
ModelRelation.addTargetEntityId(fbb, targetEntityIdOffset);
317+
if (externalNameOffset != 0) ModelRelation.addExternalName(fbb, externalNameOffset);
298318
if (externalType != 0) ModelRelation.addExternalType(fbb, externalType);
299319
return ModelRelation.endModelRelation(fbb);
300320
}
@@ -311,6 +331,7 @@ public static class EntityBuilder extends PartBuilder {
311331
private Long uid;
312332
private Integer lastPropertyId;
313333
private Long lastPropertyUid;
334+
@Nullable private String externalName;
314335
private Integer flags;
315336
@Nullable private PropertyBuilder propertyBuilder;
316337
@Nullable private RelationBuilder relationBuilder;
@@ -335,6 +356,15 @@ public EntityBuilder lastPropertyId(int lastPropertyId, long lastPropertyUid) {
335356
return this;
336357
}
337358

359+
/**
360+
* Sets the {@link ExternalName} of this entity.
361+
*/
362+
public EntityBuilder externalName(String externalName) {
363+
checkNotFinished();
364+
this.externalName = externalName;
365+
return this;
366+
}
367+
338368
/**
339369
* One or more of {@link io.objectbox.model.EntityFlags}.
340370
*/
@@ -402,6 +432,7 @@ public ModelBuilder entityDone() {
402432
@Override
403433
public int createFlatBufferTable(FlatBufferBuilder fbb) {
404434
int nameOffset = fbb.createString(name);
435+
int externalNameOffset = externalName != null ? fbb.createString(externalName) : 0;
405436
int propertiesOffset = model.createVector(propertyOffsets);
406437
int relationsOffset = relationOffsets.isEmpty() ? 0 : model.createVector(relationOffsets);
407438

@@ -417,6 +448,7 @@ public int createFlatBufferTable(FlatBufferBuilder fbb) {
417448
int idOffset = IdUid.createIdUid(fbb, lastPropertyId, lastPropertyUid);
418449
ModelEntity.addLastPropertyId(fbb, idOffset);
419450
}
451+
if (externalNameOffset != 0) ModelEntity.addExternalName(fbb, externalNameOffset);
420452
if (flags != null) ModelEntity.addFlags(fbb, flags);
421453
return ModelEntity.endModelEntity(fbb);
422454
}

0 commit comments

Comments
 (0)