0

内部に別のエンティティ(ドキュメント)へのキーを持つエンティティコースがあります。

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

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

 @Persistent private Key document;

public Document getDocument() {
  if (document != null)
  return new DocumentServiceImpl().getDocumentById(document.getId());
  return null;
 }
public void setDocument(Document document) {
  if (document != null)
   this.document = new DocumentServiceImpl().saveAndGetKey(document);
 }

一部のテストコードでは、新しいコースエンティティを作成し、新しいドキュメントエンティティを割り当てます。コースにドキュメントプロパティを設定すると、ドキュメントエンティティが保持されます。コースを永続化すると、エラーなしで永続化されますが、永続化されると、ドキュメントプロパティはnullになります。

何か案は?これが私のコースの保存機能です:

public Boolean save(Course c){
  Boolean isSaved = false;
  PersistenceManager pm = PMF.get().getPersistenceManager();

  try{   
   pm.makePersistent(c);
   isSaved = true;
  }
  catch(Exception e){
   e.printStackTrace();
   isSaved = false;
  }
  finally{
   pm.close();
  }

  return isSaved;

 }

編集して追加:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Document{
 @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

 @Persistent private String   data;
 @Persistent private Set<Key>  dTags;
 @Persistent private Date   dateCreated;
 @Persistent private Date   dateEdited;

 public Document(){
  this.dateCreated = new Date();
 }

 public Long getId() {
  if (key == null){
   return null;
  } else {
   return key.getId();
  }
 }
 public void setId(Long id) {
  if (id != null)
  key = KeyFactory.createKey(this.getClass().getSimpleName(), id);
 }

DocumentServicesImplから:

public Key saveAndGetKey(Document d) {
  try{
   if (d.getKey() == null){
    save(d);
   }

   return d.getKey();
  } catch (Exception e){
   return null;
  }  
 }

public Boolean save(Document d) {
  Boolean isSaved = false;
  PersistenceManager pm = PMF.get().getPersistenceManager();

  try {
   pm.makePersistent(d);
   isSaved = true;
  } catch (Exception e) {
   e.printStackTrace();
   isSaved = false;
  }finally{pm.close();}

  return isSaved;

 }

public Document getDocumentById(Long id){

PersistenceManager pm = PMF.get()。getPersistenceManager(); ドキュメントd=new Document();

{d = pm.getObjectById(Document.class、id);を試してください。}最後に{pm.close(); }

dを返す; }

4

1 に答える 1

1
  • Document クラスはどのようなものですか?
  • DocumentServiceImpl クラスはどのようなものですか?
  • saveAndGetKey() の単体テストはどのようなものですか? 戻り値が有効なキーであることを確認しますか? その後、データストアでそのドキュメントを検索できますか?
  • あなたの ServiceImpl クラスは PersistenceCapable ですか、それとも PersistenceAware ですか? あなたが私たちに示したものだけに基づいている必要があるかどうかはわかりません.

以下の新しいトラブルシューティングのアイデア: 次のような単純なことを試してみるとどうなりますか: とりあえず、Course.document を公開します。次に、エンティティを作成するこの簡単な方法が機能するかどうかを確認します。

pm = yourPMfactory.getPersistenceManger();
Course c = new Course();
Document d = new Document();
c.document = d;
pm.makePersistent(c);

Key myKey = c.getKey();
Course c2 = (Course) pm.getObjectById(Course.class, myKey.getId());
assertTrue(c.document != null); //or however your favorite test suite does assertions.
pm.close();
于 2009-07-14T01:03:24.227 に答える