Search code examples
javagoogle-app-enginejdo

AppEngine JDO cannot create a Persistent Object due to PMF


I'm porting my api to Java from Python using AppEngine. I've decided to use JDO. I've created my model, and now I'm trying to create a new object that's persisted to the datastore.

I'm following the instructions here: https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata

Specifically, this code in the Making Objects Persistent section

    PersistenceManager pm = PMF.get().getPersistenceManager();

    Employee e = new Employee("Alfred", "Smith", new Date());

    try {
        pm.makePersistent(e);
    } finally {
        pm.close();
    }

The problem is, I don't know where PMF came from. I'm getting a "PMF cannot be resolved" error in Eclipse.

I've looked through other problems on SO to tease out PMF's origin, and found other people do this; PersistenceManagerFactory pmf;

But when I try that, I have a problem with get(). Eclipse is saying

The method get() is undefined for the type PersistenceManagerFactory

Does anyone have a complete example of creating a JDO object and persisting it to the datastore, with all the imports and necessary variables?

Thanks in advance!!!

Here's my code so far:

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class Test_APIServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, world");
        PersistenceManagerFactory pmf;
        PersistenceManager pm = pmf.get().getPersistenceManager();
    }
}

Solution

  • You can read Google's doc at this link, which explains well enough what this PMF class is

    https://developers.google.com/appengine/docs/java/datastore/jdo/overview-dn2

    Scroll down to "Getting a PersistenceManager Instance" and there is the code. That is just one way of getting a PersistenceManagerFactory, and basic JDO doesn't need this class of Google's. Your code seems to have confused a real PersistenceManagerFactory with this wrapper class of Google's.