【问题标题】:Appengine JDO, how can I retrieve a child object knowing a child property and the parent key?Appengine JDO,如何在知道子属性和父键的情况下检索子对象?
【发布时间】:2011-08-07 16:24:05
【问题描述】:

使用持久性管理器,我如何在知道子属性和父键的情况下检索子对象?

Parent 的定义如下:

public class User {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;

@Persistent(mappedBy = "user")
@Element(dependent = "true")
private List<Section> sections;
...

而孩子是这样定义的:

public class Section {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Persistent
private User user;

@Persistent
private String title;
...

知道'User' id 和'Section' 标题,我如何检索该部分?我试图建立一个查询来检索使用这样的部分:'where title == xxx AND user.id ¿? == xxx' 但我不确定如何指定用户 ID。有没有办法使用持久性管理器中的查询或方法来做到这一点?

谢谢。

【问题讨论】:

    标签: java google-app-engine jdo datanucleus


    【解决方案1】:

    我终于用这个方法成功了:

    public static Section getSectionByTitle(String title, Key user_key){
        PersistenceManager pm = PMF.get().getPersistenceManager();  
        Query query = pm.newQuery("select from "+Section.class.getName()+" WHERE title == s && user == keyParam");
        query.declareParameters("String s, String k");
        query.setUnique(true);
        Section section = (Section) query.execute(title, user_key.getId());
        return section;
    }
    

    【讨论】:

      【解决方案2】:

      您可以在查询对象上调用此方法:

      q.setAncestor(ancestorKey);

      阅读this page 了解更多信息(祖先查询)。

      我记得看到过类似 'where ANCESTOR = ' 的语法,但我现在找不到任何参考。

      【讨论】:

      • 谢谢,但是 setAncestor 方法适用于低级数据存储 API,我尝试使用 JDO 查询。
      猜你喜欢
      • 2011-03-16
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多