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 = pm.getObjectById (document.class, id); } 마침내 {pm.close (); }
반환 d; }
해결책
- 문서 클래스는 어떻게 생겼습니까?
- DocumentServiceImpl 클래스는 어떻게 생겼습니까?
- SaveAndgetKey ()에 대한 단위 테스트는 어떤 모습입니까? 반환 값이 유효한 키인지 확인합니까? 그런 다음 데이터 저장소에서 해당 문서를 찾아 볼 수 있습니까?
- ServiceMPL 클래스가 지속 가능합니까, 아니면 영구적입니까? 그들이 당신이 우리에게 보여준 것에 기반을 두어야하는지 확실하지 않습니다.
아래의 새로운 문제 해결 아이디어 : 다음과 같은 간단한 것을 시도하면 어떻게 되는가 : 지금은 코스를 공개하십시오. 그런 다음이 더 간단한 엔티티를 만드는 방법이 작동하는지 확인하십시오.
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