【问题标题】:EntitySubclass Objectify QueryEntitySubclass 对象化查询
【发布时间】:2015-01-10 01:01:55
【问题描述】:

我正在使用带有 Objectify 4.0b3 的 Google App Engine (Java),并希望拥有一个包含子类 SportFacilityUserDefinedFacility 的超类 Facility >。我希望能够通过 id 查询 Facility,从而从所有子类中获取设施,并且我还希望能够对每个子类执行相同的操作。

@Entity
public class Facility {

    @Id
    protected Long id;
    @Index
    protected String name;
    @Index
    protected double x_coordinate;
    @Index
    protected double y_coordinate;
    @Index
    protected String address;

    protected Facility(){}

    public Facility(String name, double x_coordinate, double y_coordinate, String address) {
        this.name = name;
        this.x_coordinate = x_coordinate;
        this.y_coordinate = y_coordinate;
        this.address = address;
}

@EntitySubclass(index=true)
public class SportFacility extends Facility{

    @Index
    private String url;

    private void SportFacility(){}

    public SportFacility(String name, double x_coordinate, double y_coordinate, String address, String url) {
        super(name, x_coordinate, y_coordinate, address);
        this.url = url;
    }
}


@EntitySubclass(index=true)
public class UserDefinedFacility extends Facility{

    @Index
    private String url;

    private void UserDefinedFacility(){}

    public UserDefinedFacility(String name, double x_coordinate, double y_coordinate, String address) {
        super(name, x_coordinate, y_coordinate, address);

    }
}

基本上我的问题与Entity hierarchies in Objectify 4.0 中描述的相同,但在我的情况下是查询

Facility facility = ofy().load().type(Facility.class).id(id);

不起作用,因为 AndroidStudio 抱怨类型应该是

LoadResult<Facility> 

而不是设施。

由于继承概念的语法似乎在 Objectify 版本之间经常发生变化,我找不到可行的解决方案,所以我非常感谢任何帮助!

提前致谢

塞缪尔

【问题讨论】:

    标签: java google-app-engine android-studio google-cloud-datastore objectify


    【解决方案1】:

    Android Studio 是正确的;要获取实体本身,您需要:

    Facility facility = ofy().load().type(Facility.class).id(id).now();

    Facility facility = ofy().load().type(Facility.class).id(id).safe();

    如果实体不存在,后者将抛出 NotFoundException(我记得现在会返回 null)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      相关资源
      最近更新 更多