应用引擎 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
似乎继承类中的字段在数据存储区查询中不可用。
这实际上可以通过语法的变化或注释来实现吗?