<?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>/home/nick &#187; iBatis</title>
	<atom:link href="http://whatwouldnickdo.com/wordpress/category/ibatis/feed/" rel="self" type="application/rss+xml" />
	<link>http://whatwouldnickdo.com/wordpress</link>
	<description>Discussions on Programming, Linux, Android, and whatever else!!!</description>
	<lastBuildDate>Tue, 29 Mar 2011 20:18:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>iBatis &#8211; Mapping Database NULL</title>
		<link>http://whatwouldnickdo.com/wordpress/491/ibatis-mapping-database-null/</link>
		<comments>http://whatwouldnickdo.com/wordpress/491/ibatis-mapping-database-null/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 00:49:14 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[iBatis]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=491</guid>
		<description><![CDATA[iBatis is a framework used to map a SQL database to POJOs (Plain Old Java Objects).&#160; iBatis is one framework of many out there, others include Hibernate.&#160; Recently I&#8217;ve been tasked with a new webapp which will connect to a very old legacy database.&#160; One of the first things I decided to do was look [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ibatis.apache.org/">iBatis</a> is a framework used to map a SQL database to <a href="http://en.wikipedia.org/wiki/POJO">POJOs</a> (Plain Old Java Objects).&nbsp; iBatis is one framework of many out there, others include <a href="http://www.hibernate.org/">Hibernate</a>.&nbsp; Recently I&#8217;ve been tasked with a new webapp which will connect to a very old legacy database.&nbsp; One of the first things I decided to do was look through the database and figure out what data I needed, and then design and create java objects to hold said data.&nbsp; I chose iBatis for this particular project because I was dealing with a legacy database which I had little control over, and iBatis can give me the flexibility I need in case I need to write complex and specific SQL to construct my objects.&nbsp; <br />
<span id="more-491"></span><br />
<strong><span style="font-size: medium;">The issue:</span></strong><br />
One thing I noticed about the database was that there are NULLs littered everywhere, and using a default value for columns was barely used at all.&nbsp; So there are plenty of columns with a data type of <strong>int</strong>, <strong>float</strong> and <strong>bit</strong> (I&#8217;m using a MS SQL Server database) that have NULLs in there.&nbsp; Now, if I was recoding other applications that updated and inserted into these places, I would have checked data and inserted a default value, as well as maybe altering the column to allow no NULLs and set a default value, but I can&#8217;t do that.&nbsp; These tables I&#8217;m using will get mapped to a POJO, and of course I want the ints and booleans in the database mapped to ints and booleans in my POJO.&nbsp; But what happens if one of those database ints or booleans has a NULL?&nbsp; Well, java will try to set the int or boolean to a null (which can&#8217;t be done) and it throws an exception instead&#8230;an <strong>IllegalArgumentException</strong> to be exact.</p>
<p><strong><span style="font-size: medium;">The quick fix:</span></strong><br />
Going through the iBatis docs can sometimes be tedious.&nbsp; Fortunately, the solution is simple.&nbsp; If you open your xml file dealing with the object in question, and go to your <strong>resultMap</strong> element for the class, you&#8217;ll typically have a <strong>result</strong> element for each column you&#8217;re dealing with.&nbsp; Add an attribute called <strong>nullValue</strong> with a value to give the object in question in case a NULL is present.&nbsp; It&#8217;s that simple.&nbsp; </p>
<p><strong><span style="font-size: medium;">Here&#8217;s an example&#8230;</span></strong><br />
You have a table called Employee with several columns and create a POJO like below to hold that data:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Employee <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> login<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> password<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> authorityType<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> passwordExpired<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// ... constructors and setter/getter methods here...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Let&#8217;s say that authorityType and passwordExpired were NULL values in the database.&nbsp; Adding the nullValue attribute to the xml file like so would solve your issue:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resultMap</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;EmployeeResult&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;Employee&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;id&quot;</span>              <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;id&quot;</span>                               <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;login&quot;</span>           <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;login&quot;</span>                            <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;password&quot;</span>        <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;password&quot;</span>                         <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;authorityType&quot;</span>   <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;authorityType&quot;</span>   <span style="color: #000066;">nullValue</span>=<span style="color: #ff0000;">&quot;1&quot;</span>    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;passwordExpired&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;passwordExpired&quot;</span> <span style="color: #000066;">nullValue</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resultMap<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/491/ibatis-mapping-database-null/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

