2012-05-10 15 views
1

私は、Javaアプリケーションエンジンオブジェクトをリストプロパティを通じてモデル化して、効率的なクエリを効率的に実行しようとしています。このコードを改善することはできますか?Google App Engine用Javaのリストプロパティ - これは最も効率的でパフォーマンスの高いJDOコードですか?

私は本当に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 
} 

答えて

1

What's wrong with the co de?

あなたは@Persistentを全面に貼り付けていますが(Googlesのドキュメントには暗黙のうちに信じられないようにしてください)、その必要はありません...すべての標準タイプはデフォルトで永続的です(JDO spec、そして実際にはGoogleのコードで拡張され、Keyクラスなどをデフォルトで永続的にする)。クラスをより普通に見えるようにします。

あなたはmakePersistentを呼び出していますが、永続化されるオブジェクトはどのような「状態」ですか? JDOHelper.getObjectState(obj)が通知します。それが既に管理されているならば、それを行う必要はありません...そのバイトコード拡張プロセスの全体的なポイントは、変更を検出するためです。明らかに、非トランザクション更新はすぐには起こりません。あなたはその周りの取引を使用しているかどうかは言いません。

ログを検索すると、誰でもここでできることがわかります。

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

+0

私はGAEのJDOプラグインのバージョン2.3を使用しています。私はモデルオブジェクトの "作成"のための周囲のコードでトランザクションの私の使用をきれいにすることに基づいて問題1を解決しました。しかし、私はまだJDOObjectNotFoundExceptionsを取得している検索で問題はありますが、オブジェクトがそこにあることがわかります!私はこのアップデートで写真を投稿します。 –

+0

JDOのFYI v2.3は、GAE JDOプラグイン(datanucleus-appengine jar)のv1.0を意味します。 – DataNucleus

関連する問題