In my code I put some base fields in base abstract class 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:
@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. And I get an error:
Error:[ObjectBox] Code generation failed: No ID property found for Entity User (package:...)
How can I use objectbox with an inheritance?
Polymorphism of entities is not supported at this time, but there is a feature request.
If it helps, you can go for an interface. E.g.:
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.
Update: ObjectBox 1.4 introduced entity inheritance (non-polymorphic).