Please note that the contents
of this offline web site may be out of date. To access the most
recent documentation visit the
online version
.
Note that links that point to online resources are
green
in color and will open in a new
window.
We would love it if you could give us feedback about this material
by filling
this form
(You have to be online to fill it)
In a web application project, a JPA implementation can be specified and configured as a project facet. Facets are part
of the
Eclipse Web Tools Platform (WTP)
plugin.
To create a EclipseLink, JPA-enabled Cloud SQL web application:
Create a new web application project and configure Cloud SQL for the project by
following
these instructions
.
Right-click the Google Plugin for Eclipse project and under
Properties > Project
Facets
, select the JPA facet.
Click
Further configuration available
.
In the
Modify Faceted Project
dialog:
Select the appropriate Platform (such as
Generic 2.1
).
Select the JPA implementation type as
User Library
Specify a JPA implementation of your choice. If there are no implementations available,
click the
Manage libraries
icon and add a library.
Click
OK
to save the configuration.
In the project
appengine-web.xml
file, define the connection URLs for your local database instance (
cloudsql.url.dev
) and Cloud SQL
instance (
cloudsql.url
).
Provide values for
<your-project-id>
,
<your-instance-name>
, and
<your-database-name>
in the code.
Create entity classes (POJOs) that need to be persisted, and annotate them with JPA annotations.
Example
Greeting
entity:
/**
* Copyright 2012 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.appengine.demos;
import java.util.Date;
import javax.persistence.*;
@Entity
@Table(name = "Greeting")
public class Greeting {
private Long id;
private String author;
private Date date;
private String content;
public Greeting() {
}
public Greeting(String author, Date date, String content) {
this.author = author;
this.date = date;
this.content = content;
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date")
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Create servlet code or JSPs to use the Entity class.
Use the
coding pattern
shown previously for connecting. Note that the code also uses
either the
cloudsql.url.dev
and
cloudsql.url
property depending on where
the web application is running.
Example servlet using the
Greeting
entity:
/**
* Copyright 2012 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.appengine.demos;
import com.google.appengine.api.utils.SystemProperty;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.*;
public class EclipselinkJpaServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
res.setContentType("text/plain");
Map<String, String> properties = new HashMap();
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
properties.put("javax.persistence.jdbc.driver",
"com.mysql.jdbc.GoogleDriver");
properties.put("javax.persistence.jdbc.url",
System.getProperty("cloudsql.url"));
} else {
properties.put("javax.persistence.jdbc.driver",
"com.mysql.jdbc.Driver");
properties.put("javax.persistence.jdbc.url",
System.getProperty("cloudsql.url.dev"));
}
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"Demo", properties);
// Insert a few rows.
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new Greeting("user", new Date(), "Hello!"));
em.persist(new Greeting("user", new Date(), "Hi!"));
em.getTransaction().commit();
em.close();
// List all the rows.
em = emf.createEntityManager();
em.getTransaction().begin();
List<Greeting> result = em
.createQuery("SELECT g FROM Greeting g")
.getResultList();
for (Greeting g : result) {
res.getWriter().println(
g.getId() + " " +
g.getAuthor() + "(" + g.getDate() + "): " +
g.getContent());
}
em.getTransaction().commit();
em.close();
}
}
Under
src > META-INF
, open up
persistence.xml
, add the Entities to be persisted under
Managed Classes,
and
specify the correct name of the persistence unit as given in the
EntityManagerFactory
.
You are all set! Go ahead and run your application on the local development server for testing, and then deploy to App Engine.
For more background on Java persistence using Cloud SQL and App Engine, see: