AppEngine 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 query = pm.newQuery("select from " + Book.class.getName() + " where author == param"); query.declareParameters("文字列パラメーター"); List results = (List) query.execute("George Orwell");
Book で定義されたフィールド 'author' を照会しているため、これはオブジェクトを返します。
ただし、これは機能しません。
Query query = pm.newQuery("select from " + Book.class.getName() + " where title == param"); query.declareParameters("文字列パラメーター"); リスト結果 = (リスト) 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
継承されたクラスのフィールドがデータストア クエリで使用できないようです。
これは、構文のバリエーションや注釈を使用して実際に可能ですか?