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. 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.
@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
Facility facility = ofy().load().type(Facility.class).id(id);
does not work because AndroidStudio complains that the type should be
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!
Thanks in advance
Samuel
Android Studio is correct; 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 returnnull
as I recall).