简体   繁体   English

EntitySubclass对象化查询

[英]EntitySubclass Objectify Query

I am using Google App Engine (Java) with Objectify 4.0b3 and want to have a superclass Facility with the child classes SportFacility and UserDefinedFacility . 我正在将Google App Engine(Java)与Objectify 4.0b3配合使用,并希望具有子类SportFacilityUserDefinedFacility的超类Facility I want to be able to query for Facility by id and thereby get Facilities from all child classes and I also want to be able to do the same for each of the child classes. 我希望能够通过id查询设施,从而从所有子类中获得设施,并且我也希望能够对每个子类都做同样的事情。

@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);

    }
}

Basically my problem is the same as described in Entity hierarchies in Objectify 4.0 but in my case the query 基本上,我的问题与Objectify 4.0中的实体层次结构中描述的问题相同,但在我的情况下,查询

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

does not work because AndroidStudio complains that the type should be 无法工作,因为AndroidStudio抱怨该类型应为

LoadResult<Facility> 

instead of Facility. 而不是设施。

Since the syntax of inheritance concepts seems to change often between Objectify versions, I couldn't find a working solution, so I'd really appreciate any help! 由于继承概念的语法似乎在Objectify版本之间经常变化,因此我找不到有效的解决方案,因此,我非常感谢您的帮助!

Thanks in advance 提前致谢

Samuel 塞缪尔

Android Studio is correct; Android Studio是正确的; to get the entity itself, you want either: 要获取实体本身,您需要:

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

or 要么

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

The latter will throw a NotFoundException if the entity does not exist (now would return null as I recall). 如果该实体不存在,后者将抛出NotFoundException (现在我记得会返回null )。

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

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