JPA Tutorial with Google App Engine GAE

Here is the shortest possible tutorial for using JPA with Google App Engine.

1. Create a new Web Application Project

Use this package name:

  • Project Name: JPAGae
  • Package: mystuff
  • uncheck Use Google Web Toolkit

2. Create in the mystuff subdirectory

package mystuff;
import javax.persistence.*;

public class Employee {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double salary;

    // for ORMs which require no-arg ctr:
    public Employee() {
    public String toString() {
        return ("Employee#:" + id + ", name:" + name + ", salary:" + salary);
    // the rest are just setters and getters
    public void setId(Long id) { = id;
    public Long getId() {
        return id;
    public void setName(String name) { = name;
    public String getName() {
        return name;
    public void setSalary(double salary) {
        this.salary = salary;
    public double getSalary() {
        return salary;

3. Create

package mystuff;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class EMF {
    private static final EntityManagerFactory emfInstance =

    private EMF() {}

    public static EntityManagerFactory get() {
        return emfInstance;

4. Create persistence.xml and place it under META-INF directory

Please note, ensure that there are no blank lines at the beginning of this file

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns=""
    xsi:schemaLocation="" version="1.0">

    <persistence-unit name="myemployee">
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>

5. Add these lines to

import java.util.List;
import javax.persistence.*;

6. Add the following to your doGet method and run it as a web application

        PrintWriter out = resp.getWriter();
        out.println("Hello, world");
        EntityManager em = EMF.get().createEntityManager();

        Employee employee = new Employee();
        double x = Math.random();
        employee.setName("Tim BuckToo" + x);
        employee.setSalary(x * 100);
        try {
            // retrieve all records on file
            Query q = em.createQuery("select m from Employee m");
            List<Employee> lis = q.getResultList();
            System.out.println("found:" + lis.size());
            for (Employee e: lis) {
                out.println("found:" + e.getName() + " " + e.getSalary());
                out.println("   id:" + e.getId());

            // store current one
        } finally {

7. Run and browse

Right-click on the project, and run it as a Web Application Project. Open your browser to
http://localhost:8888 and follow the link

You will note that the first time you visit that “site”, you won’t see the data from the first doGet.
The doGet will show data from the previous visit and THEN it stores your current data.


About SunKing2

Former web developer, guitar player, started Piano 2012 -yes still doing it! Recovery, sobriety, mental health advocate.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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