![](/img/trans.png)
[英]In Android (or Java itself), is it possible to a class to inherit from a class referenced by name?
[英]Objectbox inherit properties from base class (which is NOT an entity itself)
我有一个简单的基类,我想在其中包含一些公共字段,例如 id 等。基类本身不是实体。
public class Base {
@Id
protected long id;
protected String someOtherCommonProperty;
}
我有一个实体类,扩展了基类。
@Entity
public class Entity extends Base {
String name;
String address;
}
我希望实体类从基类继承字段,但我得到的是
[ObjectBox] No @Id property found for 'Entity', add @Id on a not-null long property.
除了使用接口并且有很多重复的代码之外,还有什么方法可以解决这个问题?
您可以使用@BaseEntity
注释。
查看文档: Objectbox - Entity Inheritence 。
无耻的副本以供日后参考:
除了
@Entity
注解之外,我们@BaseEntity
为基类引入了@BaseEntity
注解,它可以代替@Entity
。 有三种类型的基类,它们是通过注解定义的:
@BaseEntity
:属性在子类中被考虑持久化,但基类本身不能持久化。@Entity
:属性被考虑在子类中持久化,基类本身是一个通常持久化的实体。例子:
// base class:
@BaseEntity
public abstract class Base {
@Id long id;
String baseString;
public Base() {
}
public Base(long id, String baseString) {
this.id = id;
this.baseString = baseString;
}
}
// sub class:
@Entity
public class Sub extends Base {
String subString;
public Sub() {
}
public Sub(long id, String baseString, String subString) {
super(id, baseString);
this.subString = subString;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.