Entity Inheritance
How to inherit properties from entity super classes.

ObjectBox - Entity Super Classes

Only available for Java/Kotlin at the moment
ObjectBox allows entity inheritance to share persisted properties in super classes. The base class can be an entity or non-entity class. For this purpose the @Entity annotation is complemented by the @BaseEntity annotation. There are three types of super classes, which are defined via annotations:
  • No annotation: The base class and its properties are not considered for persistence.
  • @BaseEntity: Properties are considered for persistence in sub classes, but the base class itself cannot be persisted.
  • @Entity: Properties are considered for persistence in sub classes, and the base class itself is a normally persisted entity.
For example:
Java
Kotlin
1
// Superclass:
2
@BaseEntity
3
public abstract class Base {
4
5
@Id long id;
6
String baseString;
7
8
public Base() {
9
}
10
11
public Base(long id, String baseString) {
12
this.id = id;
13
this.baseString = baseString;
14
}
15
}
16
17
// Subclass:
18
@Entity
19
public class Sub extends Base {
20
21
String subString;
22
23
public Sub() {
24
}
25
26
public Sub(long id, String baseString, String subString) {
27
super(id, baseString);
28
this.subString = subString;
29
}
30
}
Copied!
1
// Note: Kotlin data classes do not support inheritance,
2
// so this example uses regular Kotlin classes.
3
4
// Superclass:
5
@BaseEntity
6
abstract class Base {
7
@Id
8
var id: Long = 0
9
var baseString: String? = null
10
11
constructor()
12
constructor(id: Long, baseString: String?) {
13
this.id = id
14
this.baseString = baseString
15
}
16
}
17
18
// Subclass:
19
@Entity
20
class Sub : Base {
21
var subString: String? = null
22
23
constructor()
24
constructor(id: Long,
25
baseString: String?,
26
subString: String?) : super(id, baseString) {
27
this.subString = subString
28
}
29
}
Copied!
The model for Sub, Sub_, will now include all properties: id , baseString and subString .
It is also possible to inherit properties from another entity:
Java
Kotlin
1
// Entities inherit properties from super entities.
2
@Entity
3
public class SubSub extends Sub {
4
5
String subSubString;
6
7
public SubSub() {
8
}
9
10
public SubSub(long id, String baseString,
11
String subString, String subSubString) {
12
super(id, baseString, subString);
13
this.subSubString = subSubString;
14
}
15
}
Copied!
1
// Entities inherit properties from super entities.
2
@Entity
3
class SubSub : Sub {
4
var subSubString: String? = null
5
6
constructor()
7
constructor(id: Long,
8
baseString: String?,
9
subString: String?,
10
subSubString: String?) : super(id, baseString, subString) {
11
this.subSubString= subSubString
12
}
13
}
Copied!

Notes on usage

  • It is possible to have classes in the inheritance chain that are not annotated with @BaseEntity. Their properties will be ignored and will not become part of the entity model.
  • It is not generally recommend to have a base entity class consisting of an ID property only. E.g. Java imposes an additional overhead to construct objects with a sub class.
  • Depending on your use case using interfaces may be more straightforward.

Restrictions

  • Superclasses annotated with @BaseEntity can not be part of a library.
  • There are no polymorphic queries (e.g. you cannot query for a base class and expect results from sub classes).
  • Currently any superclass, whether it is an @Entity or @BaseEntity, can not have any relations (like a ToOne or ToMany property).
Java
Kotlin
1
// THIS DOES NOT WORK
2
@BaseEntity
3
public abstract class Base {
4
@Id long id;
5
ToOne<OtherEntity> other;
6
ToMany<OtherEntity> others;
7
}
Copied!
1
// THIS DOES NOT WORK
2
@BaseEntity
3
abstract class Base {
4
@Id
5
var id: Long = 0
6
lateinit var other: ToOne<OtherEntity>
7
lateinit var others: ToMany<OtherEntity>
8
}
Copied!