jpa4azure quick start

Your bean classes will need to follow three simple rules:
  • Annotate your bean with @Entity.
  • Provide a key field annotated with @Id.
  • Provide some properties with getter/setter methods of type int, long, double, String, boolean, Date, another @Entity type, or a Collection of entities.

Here's a simple example:

public class Book {
private String isbn;
private String title;
private String authorFirstName;
private String authorLastName;

public Book() {}

public Book(String isbn) {
    this.isbn = isbn;

/* setter/getter methods assumed */

saving a bean to Azure Table Storage

The example project comes configured so that you need to supply your own Azure credentials. The JPA specification requires that you provide a persistence.xml file located in ./META-INF/ on your classpath.

    <persistence-unit name="myazure" 
            <property name="" value="your account name goes here" />
            <property name="azure.account.key" value="your key goes here" />

Next create an entity manager. JPA will look at persistence.xml and create an entity manager connected to your Azure Storage account:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("myazure");
EntityManager em = emf.createEntityManager();

"myazure" can be anything you want, it's just the name of your persistence unit. You may have more than one, and create factories for each. This would be useful if you had 2 or more azure storage accounts. Notice above that I snuck in one method that's not part of the JPA specification, "createTables(package...)". You need to call this once for each package that contains your @Entity annotated classes. It looks into the package and for each @Entity creates a corresponding table in your Azure Storage account. If you do not call this method you'll get "TableNotFound" exceptions. Henceforth we'll use the entity manager instance "em" to perform operations with our beans.

Book b = new Book("1423140591");
b.setTitle("The Heroes of Olympus Book Two: The Son of Neptune");

After the call to "persist(b)" our newly created table row can be viewed in Cerabrata Cloud Storage Sudio
Slice 1.png

If you glance at the row you'll notice jpa4azure made some assumptions regarding your partition key. This example is the "simplest thing that can possibly work" example. Rest assured there is a way for you to control the full key of your entities.
Finding entities
Managing entity keys
hello4azure example project

Last edited Sep 12, 2011 at 1:04 AM by taylor_cowan, version 10


No comments yet.