Вот в чём проблема. Имею два класса и связь между ними один-
ко-многим.
Parent.java
@PersistenceCapable(detachable="true",
identityType=IdentityType.APPLICATION)
public class Parent{
@PrimaryKey
@Persistent
private Key id;
@Persistent
private String name;
// ту остальные поля типа Long/String
@Persistent(mappedBy = "parent")
private List<Child> childList = new ArrayList<Child>();
public Parent(String name) {
this.name= name;
this.id = KeyFactory.createKey(Parent.class.getSimpleName(), name);
}
// Тут гетеры и сеттеры для остальных полей
public String getName() {
return name;
}
public List<Child> getChildList() {
return childList;
}
}
@PersistenceCapable(detachable="true",
identityType=IdentityType.APPLICATION)
public class Child{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private Parent parent;
//........
public Child(Parent parent) {
this.parent = parent;
}
//...........
}
Объекты типа Parent добавляются отдельно и всё сохраняется и
извлекается нормально.
Объекты класса Child добавляю так:
@Transaction
public addChild(Key parentKey){
Parent parent = parentDAO.find(parentKey);
Child child = new Child(parent);
parent.getChildList().add(child);
}
Потомки добавляются и извлекаются как через:
parent.getChildList()
Так и через:
PersistenceManager pm = PMF.getPersistenceManager();
Query q = pm.newQuery(Child .class);
List<Child > childList = (List<Child >) q.execute();
Если вывести их ключи получим что-то вроде:
Parent(1)/Child(1)
Parent(1)/Child(2)
Parent(1)/Child(3)
Но требуется извлекать экземпляры класса Child по ключу, но без не
зная ни чего о родителе.
Другими словами, что бы можно было извлечь экземпляр класса Child так:
PersistenceManager pm = PMF.getPersistenceManager();
pm.getObjectById(Child.class, id);
Где id не зависит от предка.
Пробовал вместо хранения списка объектов хранить список ключей, но мне
такая идея почему-то совсе не нравится. И при попытки извлечь
экземпляры так:
PersistenceManager pm = PMF.getPersistenceManager();
Query q = pm.newQuery(Child .class, ":ids.contains(id)"
List<Child> childList = (List<Child>) q.execute((Key{})
parent.getChildIdList().toArray());
Получил ошибку приведения типов com.google.appengine.api.datastore.Key
к
com.google.appengine.api.datastore.Key
И ещё раз как извлечь предка не зная ни чего об его родителе?