简体   繁体   English

使用objectbox继承实体

[英]Inheritance in entities, using objectbox

In my code I put some base fields in base abstract class BaseEntity : 在我的代码中,我在基本抽象类BaseEntity放置了一些基本字段:

public abstract class BaseEntity {

    @Id
    private long id;

    public BaseEntity() {
    }

    public BaseEntity(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

So, in child class User I am not define an id field: 因此,在子类User我没有定义id字段:

@Entity
public class User extends BaseEntity {

    private String name;
    private String login;
    private String gender;

    private String email;
    private String phoneNumber;

    private Date registrationDate;
    private Date lastActivityDate;

    private long systemId;

    public User() {
    }

...Getters and Setters
}

because it defined in superclass. 因为它是在超类中定义的。 I don't want to create in every class an id field, and don't want to persist in database BaseEntity class. 我不想在每个类中都创建一个id字段,也不想保留在数据库BaseEntity类中。 And I get an error: Error:[ObjectBox] Code generation failed: No ID property found for Entity User (package:...) 我收到一个错误: Error:[ObjectBox] Code generation failed: No ID property found for Entity User (package:...)

How can I use objectbox with an inheritance? 如何将Objectbox与继承一起使用?

Polymorphism of entities is not supported at this time, but there is a feature request . 目前不支持实体的多态性,但是有一个功能要求

If it helps, you can go for an interface. 如果有帮助,您可以使用一个界面。 Eg: 例如:

public interface BaseEntity {
    long getId();
}

Note: you could have a setter for the ID, but my personal advice would be to have the id field package private (so ObjectBox has access from generated classes) and not have a setter because it would suggest it's OK to change the ID at any time. 注意:您可以为ID设置一个setter,但是我个人的建议是将id字段包设为私有(这样ObjectBox可以从生成的类中访问)而没有一个setter,因为这表明可以随时更改ID时间。

Update: ObjectBox 1.4 introduced entity inheritance (non-polymorphic). 更新: ObjectBox 1.4引入了实体继承 (非多态)。

@Oleg Objectbox don't support inheritance in entity class as it will map every entity to a separate table in db and use this @Id variable as unique id to identify a row(entity instance) in that table. @Oleg Objectbox不支持实体类中的继承,因为它将每个实体映射到db中的单独表,并使用此@Id变量作为唯一ID来标识该表中的行(实体实例)。 Thus @Id variable is must for every entity class. 因此,@ Id变量对于每个实体类都是必需的。

In general, 一般来说,

for each for Child class to access Parent class variables it have to be either protected or public but in your id is private so change it to either protected it will work. 对于每个供Child类访问的父类变量,它必须是受保护的或公共的,但是在您的ID中是私有的,因此将其更改为受保护的它将起作用。

    protected long id;

if you mark it is as protected only the parent and its child class can access it and when you mark it as public any class can access it. 如果将其标记为受保护,则只有父级及其子类可以访问它,而将其标记为公共时,任何类都可以访问它。 marking it as private means only parent class can access it 将其标记为私有意味着只有父类可以访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM