问题
Is there a way how to define an entity hierarchy that enables to query just particular subclass? Consider situation below. Let's have abstract Base class that defines common properties and concrete subclasses A and B.
class abstract Base {
...
}
class A extends Base {
...
}
class B extends Base {
...
}
I would like to run for example queries as follows.
To retrieve all entities of type A and B
Base base = this.objectify.load().type(Base.class).list();
To retrieve all entities of type A
Base base = this.objectify.load().type(A.class).list();
To retrieve all entities of type B
Base base = this.objectify.load().type(B.class).list();
Furthermore, we would like to store all such entities as a single type (Base entity) in GAE Datastore.
We tried to use polymorphic hierarchy of related entity classes described here:
https://code.google.com/p/objectify-appengine/wiki/Entities#Polymorphism
But it seems that this is not capable of handling a situation where there are multiple entity subclasses with a common parent.
回答1:
I don't think Base
can be abstract, but this should work:
@Entity
class Base { ... }
@EntitySubclass(index=true)
class A extends Base { ... }
@EntitySubclass(index=true)
class B extends Base { ... }
If you want to be able to query by polymorphic type, you must index the types that you want to query by.
来源:https://stackoverflow.com/questions/19210206/entity-hierarchies-in-objectify-4-0