views:

248

answers:

1

I have an entity Course that has a key to another entity (Document) inside.

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

In some test code I make a new Course entity, and assign a new Document entity, and the document entity is persisted when I set the document property on course. When I persist course, it will persist without error, however once it is persisted the document property will be null.

Any ideas? Here is my save function for course:

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;

 }

Edit to add:

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

from 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(); Document d = new Document();

try { d = pm.getObjectById(Document.class, id); } finally { pm.close(); }

return d; }

+1  A: 
  • What does the Document class look like?
  • what does the DocumentServiceImpl class look like?
  • What does your unit test for saveAndGetKey() look like? Does it check that the return value is a valid key? can you then look up that document in the datastore?
  • Are your ServiceImpl classes PersistenceCapable, or PersistenceAware? I'm not sure if they need to be or not based just on what you've shown us.

New Troubleshooting Idea below: What happens if you try something simple like this: Just for now, make Course.document public. Then see if this simpler way of creating your entities works.

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();
Peter Recore
I've edited my question above to provide the code requested.my saveAndGetKey function appears to work when stepping through with the debugger, a key value is generated.
KevMo
so after everything is said and done, you can see both the new document and the new course in the datastore, and query for both of them using their keys, but the course has null for the document? Also, i am adding one more thing to try in my answer above
Peter Recore
Not sure if it is related, but Document.setId() is using the KeyFactory to create keys, but saveAndGetKey(Document) will cause GAE to generate the key if Document.key is null. Do you want GAE to generate Document IDs or your app?
NamshubWriter
Well, that code worked. After a little bit of tweaking things it now works as expected. My test code was the problem. Thanks for the help.And NamshubWriter: I use GAE to generate the key, but I also have DTO objects that get sent to the client with long id's. Those functions make it seamless for me to transform one object into the other.
KevMo