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);
}

オブジェクトが最初にどのように格納されるかを示す残りのサポート コードと、関連するモデル クラスを次に示します。

このコードは、ハドルの作成時に呼び出されます (これにより、データストア内のすべてのオブジェクトが正しく設定されます)。

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 がいたるところに貼り付けられています (確かに Google のドキュメントで暗示されていますが、信じないのが最善です)、それは必要ありません ... すべての標準型はデフォルトで永続的です (JDO 仕様に従って、実際にKey クラスなどをデフォルトで永続化するために、Google 独自のコードによって拡張されています)。クラスがより正常に見えるようにします。

makePersistentを呼び出していますが、永続化されているオブジェクトはどの「状態」にありますか? JDOHelper.getObjectState(obj)が教えてくれます。すでに管理されている場合は、それを行う必要はありません...変更を検出するために、そのバイトコード拡張プロセスの要点です。明らかに、非トランザクション更新はすぐには行われません。その周りでトランザクションを使用しているかどうかはわかりません。

ログで回収を調べてください。ここにいる誰よりも多くのことを教えてくれます。

おそらくGAE JDOプラグインのv2.0も使用していません。それより前のものは非常に古く、維持されていません。

于 2012-05-11T06:21:04.273 に答える