<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shaun Abram &#187; money</title>
	<atom:link href="http://www.shaunabram.com/tag/money/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shaunabram.com</link>
	<description>Java and Technology weblog</description>
	<lastBuildDate>Tue, 27 Jul 2010 23:14:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Persisting Money class with Hibernate</title>
		<link>http://www.shaunabram.com/persisting-money-class-with-hibernate/</link>
		<comments>http://www.shaunabram.com/persisting-money-class-with-hibernate/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 05:09:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenSource]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[money]]></category>
		<category><![CDATA[timeandmoney]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=39</guid>
		<description><![CDATA[Further to my previous post about representing monetary amounts in Java, I have been using the TimeAndMoney library. I ran into some problems persisting my classes that use Money via Hibernate. I should point out that these are not problems with the Money class or the timeandmoney library, rather than with my hibernate setup, but [...]]]></description>
			<content:encoded><![CDATA[<p>Further to my <a href="http://www.shaunabram.com/?p=37">previous post</a> about representing monetary amounts in Java, I have been using the <a href="http://timeandmoney.sourceforge.net/">TimeAndMoney</a> library.</p>
<p>I ran into some problems persisting my classes that use Money via <a href="http://www.hibernate.org/">Hibernate</a>. I should point out that these are not problems with the Money class or the timeandmoney library, rather than with my hibernate setup, but I thought I would post my approach here for future reference.</p>
<p>I started by incorrectly trying to map my Money field (in my case, called &#8216;openingBalance) as a property, viz:</p>
<p style="padding-left: 30px;">&lt;property name=&#8221;openingBalance&#8221; type=&#8221;com.domainlanguage.money.Money&#8221;&gt;</p>
<p>Which resulted in the following error:</p>
<p style="padding-left: 30px;">SEVERE: Data truncation: Data too long for column &#8216;openingBalance&#8217; at row 1<br />
Exception in thread &#8220;main&#8221; org.hibernate.exception.GenericJDBCException: could not insert&#8230;</p>
<p>Then I tried mapping it as a component, viz</p>
<p style="padding-left: 30px;">&lt;component name=&#8221;openingBalance&#8221; class=&#8221;com.domainlanguage.money.Money&#8221;&gt;</p>
<p style="padding-left: 60px;">&lt;property name=&#8221;amount&#8221;/&gt;</p>
<p style="padding-left: 60px;">&lt;property name=&#8221;currency&#8221;/&gt;</p>
<p style="padding-left: 30px;">&lt;/component&gt;</p>
<p>But this resulted in the following error:</p>
<p style="padding-left: 30px;">Initial SessionFactory creation failed.org.hibernate.PropertyNotFoundException: Could not find a setter for property amount in class com.domainlanguage.money.Money<br />
Exception in thread &#8220;main&#8221; java.lang.ExceptionInInitializerError</p>
<p>This is because the Money class has no setAmount or setCurrency methods. The solution is to specify field access (as opposed to accessor method access), as in</p>
<p style="padding-left: 30px;">&lt;component name=&#8221;openingBalance&#8221; class=&#8221;com.domainlanguage.money.Money&#8221;&gt;</p>
<p style="padding-left: 60px;">&lt;property access=&#8221;field&#8221; name=&#8221;amount&#8221;/&gt;</p>
<p style="padding-left: 60px;">&lt;property access=&#8221;field&#8221; name=&#8221;currency&#8221;/&gt;</p>
<p style="padding-left: 30px;">&lt;/component&gt;</p>
<p>Note that if you check the Money javadocs, it (correctly) says that the setters are not provided because they would break encapsulation They do however (and I quote, &#8220;begrudgingly&#8221;) provide alternative setters such as setForPersistentMapping_Amount, but I am not sure how to get Hibernate to use these, and in this case, I don&#8217;t think it is necessary or beneficial anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/persisting-money-class-with-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Representing Money in Java</title>
		<link>http://www.shaunabram.com/representing-money-in-java/</link>
		<comments>http://www.shaunabram.com/representing-money-in-java/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 08:01:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[money]]></category>
		<category><![CDATA[timeandmoney]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=37</guid>
		<description><![CDATA[I&#8217;ve been writing some code recently that required dealing with monetary values but, as hard as it is to believe, there still doesn&#8217;t seem to be a great solution for money in Java. I&#8217;ve known for a while that float and double should never be used for representing money, or indeed anywhere exact values are [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing some code recently that required dealing with monetary values but, as hard as it is to believe, there still doesn&#8217;t seem to be a great solution for money in Java. I&#8217;ve known for a while that float and double should never be used for representing money, or indeed anywhere exact values are required (e.g. 1 &#8211; 0.9 may give you something like 0.09999999999999998). Using long or int gives exact answers but requires keeping track of the decimal point yourself, which is a pain. A common approach is to use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html">BigDecimal</a>, but that is not without its limitations either e.g. it is certainly clunkier to use than primitives, and slower, and the rules for rounding and presentation seem a little complex.</p>
<p>Another solution is to use <a href="http://timeandmoney.sourceforge.net/">timeandmoney</a>. It is a little worrying that it is still pre-alpha (v0.5.1 at time of writing), but I am going to go with it in the absence of any better contenders.</p>
<p>Note that some of this is covered in Josh Bloch&#8217;s definitive book, <a href="http://java.sun.com/docs/books/effective/">Effective Java</a>, that I <a href="http://www.shaunabram.com/?p=12">posted</a> about before, and there are some discussions about the issues <a href="http://stackoverflow.com/questions/285680/representing-monetary-values-in-java">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/representing-money-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
