[英]EntitySubclass Objectify Query
我正在將Google App Engine(Java)與Objectify 4.0b3配合使用,並希望具有子類SportFacility和UserDefinedFacility的超類Facility 。 我希望能夠通過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);
}
}
基本上,我的問題與Objectify 4.0中的實體層次結構中描述的問題相同,但在我的情況下,查詢
Facility facility = ofy().load().type(Facility.class).id(id);
無法工作,因為AndroidStudio抱怨該類型應為
LoadResult<Facility>
而不是設施。
由於繼承概念的語法似乎在Objectify版本之間經常變化,因此我找不到有效的解決方案,因此,我非常感謝您的幫助!
提前致謝
塞繆爾
Android Studio是正確的; 要獲取實體本身,您需要:
Facility facility = ofy().load().type(Facility.class).id(id).now();
要么
Facility facility = ofy().load().type(Facility.class).id(id).safe();
如果該實體不存在,后者將拋出NotFoundException
(現在我記得會返回null
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.