Google App Engine and JPA[url]http://www.vogella.com/blog/2011/01/10/google-app-engine-jpa/[/url]
在google app engine中使用JPA [url]http://www.1zhang.com/group/viewthread.php?tid=5188[/url]
1.创建的时候,会自动增加jpa的jar.
2.src/META-INF/persistence.xml
<?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="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true" />
<property name="datanucleus.NontransactionalWrite" value="true" />
<property name="datanucleus.ConnectionURL" value="appengine" />
</properties>
</persistence-unit>
<persistence-unit name="eventual-reads-short-deadlines">
<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" />
<property name="datanucleus.appengine.datastoreReadConsistency"
value="EVENTUAL" />
<property name="javax.persistence.query.timeout" value="5000" />
<property name="datanucleus.datastoreWriteTimeout" value="10000" />
</properties>
</persistence-unit>
</persistence>
3.Pojo
package com.gae.pandy.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long todoId;
private String summary;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
4.service
package com.gae.pandy.service;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class EMFService {
private static final EntityManagerFactory emfInstance = Persistence
.createEntityManagerFactory("transactions-optional");
private EMFService() {
}
public static EntityManagerFactory get() {
return emfInstance;
}
}
5.servlet
package com.gae.pandy.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gae.pandy.pojo.Todo;
import com.gae.pandy.service.EMFService;
@SuppressWarnings("serial")
public class PersistsServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
this.doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Todo todo = new Todo();
todo.setSummary("This is my todo");
EntityManager em = EMFService.get().createEntityManager();
List<Todo> todos = null;
try {
em.persist(todo);//增加到数据库
Query q = em.createQuery("select t from Todo t");
todos = new ArrayList(q.getResultList());
} finally {
em.close();
}
resp.setContentType("text/plain");
if (todos != null) {
resp.getWriter().println(
"Hello, JPA. We have " + todos.size()
+ " number of entries.");
} else {
resp.getWriter().println("Should not happen");
}
}
}
6.web.xml
<servlet>
<servlet-name>PersistsServlet</servlet-name>
<servlet-class>com.gae.pandy.controller.PersistsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PersistsServlet</servlet-name>
<url-pattern>/PersistsServlet</url-pattern>
</servlet-mapping>
执行:[url]http://localhost:8888/PersistsServlet[/url]
aopalliance-1.0.jar
appengine-api-1.0-sdk-1.7.4.jar
appengine-api-labs.jar
appengine-endpoints.jar
appengine-jsr107cache-1.7.4.jar
apps-marketplace.jar
asm-4.0.jar
commons-logging-1.1.1.jar
datanucleus-api-jdo-3.1.1.jar
datanucleus-api-jpa-3.1.1.jar
datanucleus-appengine-2.1.1.jar
datanucleus-core-3.1.1.jar
geronimo-jpa_2.0_spec-1.0.jar
jdo-api-3.0.1.jar
jsr107cache-1.1.jar
jstl.jar
jta-1.1.jar
junit-3.8.1.jar
spring-aop-3.1.2.RELEASE.jar
spring-asm-3.1.2.RELEASE.jar
spring-beans-3.1.2.RELEASE.jar
spring-context-3.1.2.RELEASE.jar
spring-context-support-3.1.2.RELEASE.jar
spring-core-3.1.2.RELEASE.jar
spring-expression-3.1.2.RELEASE.jar
spring-web-3.1.2.RELEASE.jar
spring-webmvc-3.1.2.RELEASE.jar
standard.jar