1

应用引擎 1.2.2。我像这样定义一个类 Product:

@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;
}

我像这样定义派生类 Book:

@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(new Book("乔治奥威尔", "1984"));

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

查询 query = pm.newQuery("select from " + Book.class.getName() + " where author == param"); query.declareParameters("字符串参数"); List results = (List) query.execute("George Orwell");

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

然而这不起作用:

查询 query = pm.newQuery("select from " + Book.class.getName() + " where title == param"); query.declareParameters("字符串参数"); 列表结果 = (List) query.execute("1984");

它抛出一个异常,指出没有字段“标题”,即使这是在派生类 Product 上定义的。

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

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

这实际上可以通过语法的变化或注释来实现吗?

4

2 回答 2

3

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

JDO 不支持的特性

App Engine 实现不支持 JDO 接口的以下功能:

无主关系。您可以使用显式 Key 值实现无主关系。未来的版本可能会支持 JDO 的无主关系语法。拥有多对多关系。

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

JDOQL 分组和其他聚合查询。

多态查询。您不能通过查询类来获取子类的实例。每个类都由数据存储区中的一个单独的实体类型表示。

IdentityType.DATASTORE 用于 @PersistenceCapable 注释。仅支持 IdentityType.APPLICATION。

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

于 2009-09-19T12:48:46.127 回答
1

该查询使用 DataNucleus 和我们支持的任何其他数据存储(例如 RDBMS、XML、Excel 等),确实应该允许超类中的字段;查询是有效的 JDOQL。如果它们在 GAE/J 中不起作用,则在 Google 的问题跟踪器中报告问题,尽管肯定存在关于继承的问题 http://code.google.com/p/datanucleus-appengine/issues/list

于 2009-08-11T18:49:48.217 回答