我想通过 Google AppEngine 上的 datanucleus 的父键检索数据。我使用 JPA。
这是我的 Stock bean(父级):
@Entity
public class Stock implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
@OneToMany(fetch=FetchType.LAZY, mappedBy="stock")
private List<StockValue> stockValues;
}
这是我的 StockValue bean(孩子们):
@Entity
public class StockValue implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
@ManyToOne
private Stock stock;
}
我试着做这样的请求(查询是数据核查询):
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Query queryStock = new Query("Stock");
queryStock.addFilter("name", FilterOperator.EQUAL, "toto");
PreparedQuery prepare = datastoreService.prepare(queryStock);
Entity asSingleEntity = prepare.asSingleEntity();
Query query = Query("StockValue", asSingleEntity.getKey());
prepare = datastoreService.prepare(query);
List<Entity> asList = prepare.asList(FetchOptions.Builder.withLimit(10));
一切都很好。但是我得到了实体,我更喜欢使用我的 bean。所以我尝试这样做(查询是 javax 查询):
Query createQuery = entityManager.createQuery("SELECT p FROM Stock p WHERE p.name = \"toto\"");
Object singleResult = createQuery.getSingleResult();
Stock stockEntity = (Stock)singleResult;
createQuery = entityManager.createQuery("SELECT p FROM StockValue p WHERE p.key IS NOT null AND p.key.parentKey = :parentKey ");
createQuery.setParameter(":parentKey", stockEntity.getKey());
Object singleResult2 = createQuery.getSingleResult();
这是我的问题,我收到此错误:
org.datanucleus.store.appengine.FatalNucleusUserException: SELECT FROM StockValue p WHERE p.key IS NOT null AND p.key.parentKey = :parentKey: Can only reference properties of a sub-object if the sub-object is embedded.
我不明白这一点。我的豆子之间的关系似乎很好,因为如果我这样做:
stockEntity.getStockValues();
我正确地得到了 stockValues 。为什么我的请求失败?
谢谢你的帮助