ObjectBox Docs
HomeBlogTwitterGitHub
  • ObjectBox Docs
  • Getting started
  • Tutorial: Demo Project
  • Entity Annotations
  • Android (Java/Kotlin)
    • Android Local Unit Tests
    • LiveData (Arch. Comp.)
    • Paging (Arch. Comp.)
    • App Bundle, split APKs and LinkageError
    • greenDAO Compat
  • Desktop Apps
  • Kotlin Support
  • ObjectBox Queries
  • On-Device Vector Search
  • Data Observers & Rx
  • Relations
  • ObjectBox Admin
  • Transactions
  • Advanced
    • Advanced Setup
    • Object IDs
    • Custom Types
    • Entity Inheritance
    • Data Model Updates
    • Meta Model, IDs, and UIDs
  • Changelogs
  • Java API reference
  • Dart API reference
  • Python API reference
  • Binary License
  • FAQ
  • Troubleshooting
  • Data Sync
  • Swift Database for iOS
  • C++ Database Docs
  • Java Release History (<= v1.5)
Powered by GitBook
On this page
  • ObjectBox - Entity Super Classes
  • Notes on usage
  • Restrictions

Was this helpful?

Export as PDF
  1. Advanced

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:

// Superclass:
@BaseEntity
public abstract class Base {
    
    @Id long id;
    String baseString;
    
    public Base() {
    }
    
    public Base(long id, String baseString) {
        this.id = id;
        this.baseString = baseString;
    }
}

// Subclass:
@Entity
public class Sub extends Base {
    
    String subString;
    
    public Sub() {
    }
    
    public Sub(long id, String baseString, String subString) {
        super(id, baseString);
        this.subString = subString;
    }
}
// Note: Kotlin data classes do not support inheritance,
// so this example uses regular Kotlin classes.

// Superclass:
@BaseEntity
abstract class Base {
    @Id
    var id: Long = 0
    var baseString: String? = null

    constructor()
    constructor(id: Long, baseString: String?) {
        this.id = id
        this.baseString = baseString
    }
}

// Subclass:
@Entity
class Sub : Base {
    var subString: String? = null

    constructor()
    constructor(id: Long, 
                baseString: String?,
                subString: String?) : super(id, baseString) {
        this.subString = subString
    }
}

The model for Sub, Sub_, will now include all properties: id , baseString and subString .

It is also possible to inherit properties from another entity:

// Entities inherit properties from super entities.
@Entity
public class SubSub extends Sub {
    
    String subSubString;
    
    public SubSub() {
    }
    
    public SubSub(long id, String baseString,
                  String subString, String subSubString) {
        super(id, baseString, subString);
        this.subSubString = subSubString;
    }
}
// Entities inherit properties from super entities.
@Entity
class SubSub : Sub {
    var subSubString: String? = null

    constructor()
    constructor(id: Long,
                baseString: String?,
                subString: String?,
                subSubString: String?) : super(id, baseString, subString) {
        this.subSubString= subSubString
    }
}

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).

// THIS DOES NOT WORK
@BaseEntity
public abstract class Base {
    @Id long id;
    ToOne<OtherEntity> other; 
    ToMany<OtherEntity> others; 
}
// THIS DOES NOT WORK
@BaseEntity
abstract class Base {
    @Id
    var id: Long = 0
    lateinit var other: ToOne<OtherEntity>
    lateinit var others: ToMany<OtherEntity>
}

PreviousCustom TypesNextData Model Updates

Last updated 1 year ago

Was this helpful?