Monday, August 01, 2011

Google App Engine Datastore Java BigDecimal and JDO

If you have tried to persist a BigDecimal field in the Google App Engine Datastore using the low level Java API, you may have noticed BigDecimal is not natively supported. However, through the JDO interface, it is possible to persist a BigDecimal field on your entity.

Define the entity with the proper JDO annotations.

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class MyEntity {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long id;
 
  @Persistent
  private BigDecimal amount;
}

The JDO implementation for the app engine datastore will handle the persistence of the BigDecimal value even though BigDecimal itself is not persisted.

The important thing to understand is that there is a conversion happening so the BigDecimal precision is not entirely maintained. It seems to be the same as if you converted a BigDecimal object into float or double and back to BigDecimal. So, a BigDecimal with the value 1.99 might be actually stored as something like 1.9900000000000001101341... when it is converted back to BigDecimal.