views:

57

answers:

4

I am new to GAE, and to JDO, I am getting stuck with how to update data.

Using the code below, if I do a getAll(), then a get() on an object, then alter an attribute for that object returned by get(), then a getAll(), the second call to getAll() returns the original un-altered object.

I tried doing a flush() but that doesn't seem to help. If I restart jetty, the data is not persisted.

public class Notes {

@SuppressWarnings("unchecked")
public List<Note> getAll() {
    PersistenceManager pm = PMF.instance().getPersistenceManager();

    Query query = pm.newQuery("select from com.uptecs.google1.model.Note order by subject");
    return (List<Note>) query.execute();
}

public void add(Note note) {
    PersistenceManager pm = PMF.instance().getPersistenceManager();
    pm.makePersistent(note);
    pm.flush();
}

public Note get(long id) {
    PersistenceManager pm = PMF.instance().getPersistenceManager();
    return (Note)pm.getObjectById(Note.class, id);
}

public void update(Note note) {
    PersistenceManager pm = PMF.instance().getPersistenceManager();
    pm.flush(); 
}
}
+1  A: 

Have you looked at the AppEngine Getting Started Guide? They have a pretty extensive guide on using the JDO API.

Sounds like you aren't calling close() after modifying the persistent object.

matt b
A: 

I am not very familiar with JDO but don't you have to commit() or save() your data before flush? I think only these statements will persist in the database.

Paras
+1  A: 

Perhaps actually closing your PersistenceManagers may help (never mind the memory utilisation reasons!)

DataNucleus
+3  A: 

For a good overview look at these articles:
http://www.ibm.com/developerworks/java/library/j-gaej1/
http://www.ibm.com/developerworks/java/library/j-gaej2/index.html
http://www.ibm.com/developerworks/java/library/j-gaej3.html

2 and 3 are most relevant.

public void add(Note note) {
    PersistenceManager pm = getPersistenceManagerFactory()
            .getPersistenceManager();
    try {
        pm.makePersistent(note);
    } finally {
        pm.close();
    }
}
Romain Hippeau
It's so weird that part 3's link is formatted differently, and doesn't work in a format like the other two... good articles though!
Jason Hall
I clearly misread something in the Google documentation, thanks!
Jacob