Custom Types

How to store types in ObjectBox that are not supported by default, recommendations for storing enums.

ObjectBox - Supported Types

With ObjectBox you can store pretty much any type (class), given that it can be converted to any of the built-in types.

ObjectBox can store the following built-in types without a converter:

boolean, Boolean
int, Integer
short, Short
long, Long
float, Float
double, Double
byte, Byte
char, Character
Date // Time with millisecond precision.
// As of 3.0.0-alpha2 the following work out of the box:
@Type(DateNano) long, Long // Time with nanosecond precision.
Boolean, Boolean?
Int, Int?
Short, Short?
Long, Long?
Float, Float?
Double, Double?
Byte, Byte?
Char, Char?
String, String?
Date, Date? // Time with millisecond precision.
// As of 3.0.0-alpha2 the following work out of the box:
@Type(DateNano) Long, Long? // Time with nanosecond precision.

To store any other type, configure a converter like shown below.

Convert annotation and property converter

To add support for a custom type, you can map properties to one of the built-in types using a @Convert annotation. You also need to provide a PropertyConverter implementation.

For example, you could define a color in your entity using a custom Color class and map it to an Integer. Or you can map the popular org.joda.time.DateTime from Joda Time to a Long.

Here is an example mapping an enum to an Integer:

public class User {
private Long id;
@Convert(converter = RoleConverter.class, dbType = Integer.class)
private Role role;
public enum Role {
final int id;
Role(int id) { = id;
public static class RoleConverter implements PropertyConverter<Role, Integer>; {
public Role convertToEntityProperty(Integer databaseValue) {
if (databaseValue == null) {
return null;
for (Role role : Role.values()) {
if ( == databaseValue) {
return role;
return Role.DEFAULT;
public Integer convertToDatabaseValue(Role entityProperty) {
return entityProperty == null ? null :;

Things to look out for

If you define your custom type or converter inside your entity class, they have to be static.

Don’t forget to handle null values correctly – usually you should return null if the input is null.

Database types in the sense of the converter are the primitive Java types offered by ObjectBox, as mentioned in the beginning. It is recommended to use a primitive type that is easily convertible (int, long, byte array, String, …).

You must not interact with the database (such as using Box or BoxStore) inside the converter. The converter methods are called within a transaction, so for example getting or putting entities to a box will fail.

Note: For optimal performance, ObjectBox will use a single converter instance for all conversions. Make sure the converter does not have any other constructor besides the parameter-less default constructor. Also, make it thread safe, because it might be called concurrently on multiple entities.

List/Array types

You can use a converter with List types. For example, you could convert a List of Strings to a JSON array resulting in a single string for the database. At the moment it is not possible to use an array with converters (you can track this feature request).

How to convert Enums correctly

Enums are popular with data objects like entities. When persisting enums, there are a couple of best practices:

  • Do not persist the enum’s ordinal or name: Both are unstable, and can easily change the next time you edit your enum definitions.

  • Use stable ids: Define a custom property (integer or string) in your enum that is guaranteed to be stable. Use this for your persistence mapping.

  • Prepare for the unknown: Define an UNKNOWN enum value. It can serve to handle null or unknown values. This will allow you to handle cases like an old enum value getting removed without crashing your app.

Custom types in queries

QueryBuilder is unaware of custom types. You have to use the primitive DB type for queries.

So for the Role example above you would get users with the role of admin with the query condition .equal(UserProperties.Role, 2).