代码之家  ›  专栏  ›  技术社区  ›  Jim Blackler

JavaAppEngine数据存储:如何查询对象继承类的字段?

  •  1
  • Jim Blackler  · 技术社区  · 15 年前

    见1.2.2。我这样定义一个类产品:

    @PersistenceCapable(identityType = IdentityType.APPLICATION, table="Products")
    public class Product {
    
     public Product(String title) {
      super();
      this.title = title;
     }
    
     public String getTitle() {
      return title;
     }
    
     @Persistent
     String title;
    
     @PrimaryKey
     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
     private Key id;
    }
    

    我这样定义派生类书籍:

    @PersistenceCapable(identityType = IdentityType.APPLICATION, table="Products")
    public class Book extends Product {
    
     public Book(String author, String title) {
      super(title);
      this.author = author;
     }
    
     public String getAuthor() {
      return author;
     }
    
     @Persistent
     String author;
    }
    

    然后我做了一个这样的新物体:

    persistenceManager pm=pmf.get().getPersistenceManager(); pm.makepersistent(新书(“George Orwell”,“1984”));

    我可以使用如下查询来查询这个新对象:

    query query=pm.newquery(“select from”+book.class.getname()+“where author==param”); query.declareparameters(“string param”); list results=(list)query.execute(“george orwell”);

    这将返回对象,因为我正在查询Book上定义的字段“author”。

    但是这不起作用:

    query query=pm.newquery(“select from”+book.class.getname()+“where title==param”); query.declareparameters(“string param”); list results=(list)query.execute(“1984”);

    它抛出一个异常,说明没有字段“title”,即使在派生类产品上定义了这个字段。

    javax.jdo.JDOUserException: Field "title" does not exist in com.example.Book or is not persistent
    NestedThrowables:
    org.datanucleus.store.exceptions.NoSuchPersistentFieldException: Field "title" does not exist in com.example.Book or is not persistent
    

    似乎继承类中的字段在数据存储查询中不可用。

    事实上,这在语法上是可能的,还是在注释上?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Gaurav    15 年前

    来自: http://code.google.com/appengine/docs/java/datastore/usingjdo.html

    JDO不支持的功能

    应用程序引擎实现不支持JDO接口的以下功能:

    无主关系。可以使用显式键值实现无主关系。JDO的无主关系语法可能在未来的版本中得到支持。 拥有多对多的关系。

    “加入”查询。在对父类执行查询时,不能在筛选器中使用子实体的字段。注意,可以在查询中使用键直接测试父级的关系字段。

    JDOQL分组和其他聚合查询。

    多态查询。不能执行类查询以获取子类的实例。每个类在数据存储中由一个单独的实体类型表示。

    @persistencecapable注释的identitytype.datastore。仅支持IdentityType.Application。

    当前有一个错误阻止超类上的持久字段保存到数据存储。这将在将来的版本中修复。

        2
  •  1
  •   DataNucleus    15 年前

    该查询与我们支持的任何其他数据存储(如RDBMS、XML、Excel等)一起使用DataNucleus,实际上应该允许超类中的字段;该查询是有效的JDOQL。如果他们不在gae/j中工作,那么就在谷歌的问题追踪系统中报告这个问题,尽管关于继承肯定存在问题。 http://code.google.com/p/datanucleus-appengine/issues/list