This project is read-only.

Manual keys

Azure Tables use a simple key mechanism. Each row has a partition key and a row key, and strategies for setting these depend on your application and performance requirements. jpa4azure allows you to be fully in control of both via the "SimpleKey" type. Say for instance you have a class "Article" for which you'd like to hand craft both the partition and row key. You'll need to first give your Article bean class a key field, and second, anotate it as the bean's @Id field:
@Entity
public class Article {
   @Id
   Key key;

   public void setKey(Key k) { key = k;}
   public Key getKey() { return key;}
}


Now you can simple provide a key as needed for each new Article before it's saved
Key k = new Key("partitionval1", "row1");
Article a1 = new Article();
a1.setKey(k);
em.persist(a1);

Timestamp Keys

Articles are often indexed via the time they were created. jpa4azure provides support for a "TimestampKey" that allows for temporal queries (before, after, within). This requires a very simple change to our Article class:
@Entity
public class Article {
	
	@Id
	private TimestampKey key;

The Timestamp key normally takes one constructor argument, a partition key, and creates it's own timestamp from the current time. This example creates a new Article with a timestamp based on current time, and a partition key "DEFAULT"
Article a = new Article();
a.setTitle("one");
a.setKey(new TimestampKey("DEFAULT"));


There is a 2nd overloaded constructor that takes a specific time if you want to create a timestamp in the past or future:
Calendar today = Calendar.getInstance();
today.add(Calendar.HOUR, -1);
Date minus1 = today.getTime();
TimestampKey key = new TimestampKey("DEFAULT", minus1);

Timstamp keys are advantageous when you need to query based on the recency of an entity, or it's occurance sometime in the past or between two dates. Timestamp keys provide a find() method that helps build queries:
TimestampKey key = new TimestampKey("DEFAULT");
TypedQuery<Article> query = aem.createQuery( key.find(Article.class).since(minus1) );
assertEquals(2, query.getResultList().size());

Last edited Sep 12, 2011 at 4:17 AM by taylor_cowan, version 5

Comments

No comments yet.