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)



Using Java Persistence API (JPA) with Cloud SQL

Object Relational Mapping (ORM) frameworks are popular in the Java community for accessing relational databases. Using an ORM framework can simplify porting your application between different database vendors. The Eclipse Web Tools Platform offers a robust set of tools to configure and work with ORM frameworks, such as the Java Persistence API (JPA) , with an implementation of your choice. Examples of JPA implementations include Hibernate , Data Nucleus , and EclipseLink .

Contents

Installing the Eclipse Web Tools Platform

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.

If you are using the Java EE Eclipse IDE, the plugin should already be included in Eclipse by default, and you do not have to install it again. If you need to install WTP, you can install it from the main Eclipse software repositories (such as for the Eclipse Helios Release , the Eclipse Indigo Release , the Eclipse Juno Release , or the Eclipse Kepler Release ).

Create a JPA-enabled Cloud SQL web application

This section shows how to use EclipseLink for object persistence. The full code sample is at GitHub, Google Cloud SQL/App Engine native EclipseLink JPA demo .

To create a EclipseLink, JPA-enabled Cloud SQL web application:

  1. Create a new web application project and configure Cloud SQL for the project by following these instructions .
  2. Right-click the Google Plugin for Eclipse project and under Properties > Project Facets , select the JPA facet.

  3. Click Further configuration available .
  4. 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.

  5. 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.
    <property name="cloudsql.url.dev"
                 value="jdbc:mysql://127.0.0.1:3306/<your-database-name>?user=root"/>
    <property name="cloudsql.url"
                 value="jdbc:google:mysql://<your-project-id>:<your-instance-name>/<your-database-name>?user=root";"/>
    
  6. Create entity classes (POJOs) that need to be persisted, and annotate them with JPA annotations.
  7. 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.

  8. 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:

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.