Google App Engine, JPA 2 and BigDecimal

Google App Engine supports JPA 2 so it seems one can be isolated from specific features of Datastore (Google App Engine storage service). In theory JPA has no problems with persisting BigDecimal fields. However it turns out that in JPA implementation used by Google App Engine 1.9.2 BigDecimal values are stored as double IEEE 754 floating point values by default. When restoring BigDecimal values are not equal to values that were stored! The fractional part is extended by semi-random digits (rounding issues due to IEEE 754 encoding): 0.100 becomes something like 0.1000000000000000055511151231257827021181583404541015625. There was a fix but it’s turned off by default and its usage is not well documented.

SOLUTION:
Edit your persistence.xml file and add following property:

<property name="datanucleus.appengine.BigDecimalsEncoding" value="String"/>

In effect BigDecimal values will be stored as String values underneath, what seems much better solution. The drawback I know is BigDecimals loose scale: 0.100 will become 0.1

Advertisements

About krzysztoftomaszewski

I've got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I'm working on computer software design and engineering continuously since 2004.
This entry was posted in GoogleAppEngine, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s