Objectify 4.0中的实体层次结构 [英] Entity hierarchies in Objectify 4.0

查看:128
本文介绍了Objectify 4.0中的实体层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法如何定义一个实体层次结构,使其能够查询特定的子类?考虑下面的情况。让我们有抽象Base类,它定义了公共属性和具体的子类A和B.

  class abstract Base {
.. 。
}

class A扩展基础{
...
}

class B扩展基础{
.. 。
}

我想按以下方式运行示例查询。


$ b

  • 检索类型A和B的所有实体

      Base base = this.objectify.load()。type(Base.class).list(); 


  • 检索A类型的所有实体

      Base base = this.objectify.load()。type(A.class).list(); 


  • 检索B类型的所有实体

      Base base = this.objectify.load()。type(B.class).list(); 




此外,我们希望将所有实体作为GAE Datastore中的单一类型(Base实体)。



我们尝试使用此处描述的相关实体类的多态层次结构:

https://code.google.com/p / objectify-appengine / wiki / Entities#Polymorphism



但似乎无法处理存在多个实体子类的情况我不认为 Base 可以是抽象的,但这应该工作:

  @Entity 
class Base {...}

@EntitySubclass(index = true)
class A extends Base {...}

@EntitySubclass(index = true)
class B extends Base {...}

如果您希望能够通过多态类型,你必须索引你想查询的类型。


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.

解决方案

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.

这篇关于Objectify 4.0中的实体层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆