Search

Wednesday, June 29, 2011

Google Application Engine (GAE) basic - Storing Data

Google Application Engine, GAE, is a web service set provided by Google. It is free for small web application - which is more than enough to start a web application.

Objective
Learn how to store data in Google Application Engine, using Java Persistence Annotation (JPA).

Storing Data in GAE
An application creates entities, with data values stored as properties of an entity. The app can perform queries over entities. All queries are pre-indexed for fast results over very large data sets.

Datastore entities are schemaless: Two entities of the same kind are not obligated to have the same properties, or use the same value types for the same properties. The application is responsible for ensuring that entities conform to a schema when needed.
The datastore provides a low-level API with simple operations on entities, including getputdelete, and query. You can use the low-level API to implement other interface adapters, or just use it directly in your applications.



source : http://code.google.com/appengine/docs/java/datastore/overview.html

Java Persistence Annotation - JPA

Java Persistence API (JPA) is a standard interface for storing objects containing data into a relational database. The standard defines interfaces for annotating Java objects, retrieving objects using queries, and interacting with a database using transactions. An application that uses the JPA interface can work with different databases without using any vendor-specific database code. JPA simplifies porting your application between different database vendors.
The App Engine Java SDK includes an implementation of JPA 1.0 for the App Engine datastore. The implementation is based on DataNucleus Access Platform. JPA presents a standard interface for interacting with relational databases, but the App Engine datastore is not a relational database. As a result, there are features of JPA that the App Engine implementation simply cannot support. We have done our best to call attention to these features wherever possible.


source : http://code.google.com/appengine/docs/java/datastore/jpa/overview.html

Starting JPA for GAE

  • Make sure JPA and Datastore JAR's available in build path, war/WEB-INF/lib/ directory.
  • Create persistence.xml file in war/WEB-INF/classes/META-INF/directory. persistence.xml is a configuration file to tell JPA to use GAE Datastore. Content of 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.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>
  • The project's build process must perform a post-compilation "enhancement" step on the compiled data classes to associate them with the JPA implementation.
Local Developement (DevAppServer)
GAE admin for development (local) server can be accessed by url http://localhost:8888/_ah/admin
This admin console can be used for managing Datastore, Task Queues, XMPP, and Inbound Mail.


source : http://code.google.com/appengine/docs/java/datastore/jpa/overview.html
http://www.vogella.de/articles/GoogleAppEngineJava/article.html