Google DataStoreな保存子どもオブジェ
-
13-09-2019 - |
質問
私法コースとする重要な役割を担って別のエンティティ(文書)です。
@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;
}
公文書getDocumentById(Long id){
PersistenceManager pm=PMF.get().getPersistenceManager();文書d=新しい書類();
try{ d=時までです。getObjectById(Document.class id);}finally{ 時までです。close();}
返d;}
解決
- どのようなドキュメントクラスを見 のでしょう?
- どのようなDocumentServiceImplクラスの音も聞こえてきます。
- なおユニットテスト saveAndGetKey()どうなっているか。なることを確認の返り値が有効なキー?きしみのドキュメントのdatastore?
- おServiceImpl授業 PersistenceCapable、 PersistenceAware?私のプレの設定顔は丸れなければならないとなると、はないかんを示します。
新ュア。何が起こっている場合も簡単にこのように:だっくコースです。文書公開します。そしてこのように作成団体です。
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();
所属していません StackOverflow