Converting my GAE application from JDO to JPA

June 25, 2011 at 2:38 pm Leave a comment

In past I had used the GAE with JDO, and in this exercise I will convert it to JPA.

In javasource/META-INF is the jdoconfig.xml:

<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
  <persistence-manager-factory name="transactions-optional">
    <property name="javax.jdo.PersistenceManagerFactoryClass" 
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
    <property name="javax.jdo.option.ConnectionURL" value="appengine"/>
    <property name="javax.jdo.option.NontransactionalRead" value="true"/>
    <property name="javax.jdo.option.NontransactionalWrite" value="true"/>
    <property name="javax.jdo.option.RetainValues" value="true"/>
    <property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
  </persistence-manager-factory>
</jdoconfig>

private static final PersistenceManagerFactory PMF =
JDOHelper.getPersistenceManagerFactory(“transactions-optional”);

Example write:

PersistenceManager pm = getPersistenceManager();
try {
  pm.makePersistent(new Movie(getUser(), symbol, note));
} finally {
  pm.close();
}

Example read and delete:

Query q = pm.newQuery(Movie.class, "user == u");
q.declareParameters("com.google.appengine.api.users.User u");
List<Movie> movies = (List<Movie>) q.execute(getUser());
for (Movie movie : movies) {
if (moviename.equals(movie.getSymbol())) {
    deleteCount++;
    pm.deletePersistent(movie);
}

Now to JPA queries:

 

private static final EntityManagerFactory EMF =
      Persistence.createEntityManagerFactory("transactions-optional");
private EntityManager getEntityManager() {
	    return EMF.createEntityManager();
	  }
EntityManager em = getEntityManager();
		// Read the existing entries
		javax.persistence.Query q = em.createQuery("select m from Movie m");


@Entity
public class Movie {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
Advertisements

Entry filed under: Uncategorized.

Places and Activity – advancing MVP model Developer Sharing June Edition

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


@davidmbloom

Error: Twitter did not respond. Please wait a few minutes and refresh this page.


%d bloggers like this: