1

我正在尝试通过列表属性对我的 Java 应用程序引擎对象进行建模,以实现快速高效的查询。我可以改进这段代码吗?

我真的不想迁移到 Objectify。

这是检索它的代码:

query = persistenceManager.newQuery(HuddleCreatorIndex.class, ":p.contains(creator)");
List<HuddleCreatorIndex> myCreatorIndexList = (List<HuddleCreatorIndex>) query.execute(KeyFactory.createKey(Contact.class.getSimpleName(), fbUserId));
if (myCreatorIndexList.size() > 0) {
    Set<Long> huddles = myCreatorIndexList.get(0).getHuddles();
    //THIS FOR LOOP DID THE TRICK, why is this necessary?  Never saw it in any examples
    for (Long huddleKey : huddles)
        alist.add(persistenceManager.newObjectIdInstance(Huddle.class, huddleKey));
    Collection<Huddle> huddlesICreated = persistenceManager.getObjectsById(huddles);
    allMyHuddles.addAll(huddlesICreated);
}

以下是显示对象最初存储方式的其余支持代码,以及相关的模型类。

此代码在创建 Huddle 时调用(这会正确设置数据存储中的所有对象)

txn.begin();
//First, create the Huddle, this main model class
newHuddle.setInvitees(inviteeList);
newHuddle = pm.makePersistent(newHuddle);
huddleId = newHuddle.getId();

//Now create the first index
HuddleIndex newIndex = new HuddleIndex();               
newIndex.setParent(KeyFactory.createKey(Huddle.class.getSimpleName(), newHuddle.getId()));
newIndex.setInvitees(inviteeList);
pm.makePersistent(newIndex);

//And finally, create the other index
Query query = pm.newQuery(HuddleCreatorIndex.class);
query.setFilter(":p.contains(creator)");
List<HuddleCreatorIndex> list = (List<HuddleCreatorIndex>) query.execute(creator.getId());
if (list != null && list.size() > 0) {
    HuddleCreatorIndex thisIndex = list.get(0);
    Set<Key> huddles = thisIndex.getHuddles();
    huddles.add(KeyFactory.createKey(Huddle.class.getSimpleName(), newHuddle.getId()));
    thisIndex.setHuddles(huddles);
} else {
    HuddleCreatorIndex newCreatorIndex = new HuddleCreatorIndex();
    newCreatorIndex.setCreator(creator.getId());
    Set<Long> huddleSet = new HashSet<Long>(1);                   
    huddleSet.add(newHuddle.getId());
    newCreatorIndex.setHuddles(huddleSet);
    pm.makePersistent(newCreatorIndex);
}
txn.commit();

这是我的实际模型对象与真实数据

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Huddle {

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

    private Key creator;
    private String name;
    private Date whenTimeStamp;
    private Date creationTimeStamp;
    private String userIdType;
    private Set<Key> invitees;
    private String status;
    private Set<Key> choices;
    private List<Vote> votes;

    //setters, getters, and constructor omitted
}

我的索引对象使列表属性魔术发生

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class HuddleCreatorIndex {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id; 
    private Key creator;
    private Set<Long> huddles;
    //setters, getters, and constructor omitted
}
4

1 回答 1

1

代码有什么问题?

您在整个地方都贴满了@Persistent(诚然,谷歌文档暗示,但最好不要相信它们),您不需要...所有标准类型默认情况下都是持久的(根据 JDO 规范,实际上由 Google 自己的代码扩展,以使其 Key 类等默认为持久)。使课堂看起来更加正常。

您正在调用makePersistent,但被持久化的对象处于什么“状态”?JDOHelper.getObjectState(obj)告诉你。如果它已经被管理,那么就没有必要这样做了......那个字节码增强过程的重点是检测变化。显然,非事务性更新不会立即发生。你不会说是否使用围绕它的交易。

在日志中查找检索,比这里的任何人都告诉你更多。

可能也没有使用 GAE JDO 插件的 v2.0;早于此的任何东西都非常陈旧且未维护。

于 2012-05-11T06:21:04.273 回答