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 Employee.java in the mystuff subdirectory


package mystuff;
import javax.persistence.*;

@Entity
public class Employee {
    
    @Id
    @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) {
        this.id = id;
    }
    public Long getId() {
        return id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public double getSalary() {
        return salary;
    }
}

3. Create EMF.java


package mystuff;

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

public final class EMF {
    private static final EntityManagerFactory emfInstance =
        Persistence.createEntityManagerFactory("myemployee");

    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="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="myemployee">
        <provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
        </properties>
    </persistence-unit>
    
</persistence>

5. Add these lines to JpaGaeServlet.java


import java.io.PrintWriter;
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
            em.persist(employee);
        } finally {
            em.close();
        }
        out.println("bye!");

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.

Advertisements

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:

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