Entity hierarchies in Objectify 4.0

心已入冬 提交于 2019-12-13 05:37:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!