<?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>lstierneyltd &#187; ibatis</title>
	<atom:link href="http://lstierneyltd.com/blog/tag/ibatis/feed/" rel="self" type="application/rss+xml" />
	<link>http://lstierneyltd.com/blog</link>
	<description>Yet another development blog</description>
	<lastBuildDate>Wed, 13 Jul 2011 12:55:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Using Ibatis Typehandlers to fix Oracle Date &#8220;missing time&#8221;</title>
		<link>http://lstierneyltd.com/blog/development/examples/using-ibatis-typehandlers-to-fix-oracle-date-missing-time/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/using-ibatis-typehandlers-to-fix-oracle-date-missing-time/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 11:25:44 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[ibatis]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=160</guid>
		<description><![CDATA[The Problem When using Ibatis to retrieve Date type values, on Oracle 10g (some driver versions), the time portion of the value returned from the database is not mapped onto the Java object. i.e. In DB: 02-JAN-10 13:30:00 In Java Object after mapping: 02-JAN-10 00:00:00 The Ibatis ResultMapper, snippet, looks like this: .... &#60;result property="savedDate" [...]]]></description>
			<content:encoded><![CDATA[<h4>The Problem</h4>
<p>When using Ibatis to retrieve Date type values, on Oracle 10g (some driver versions), the time portion of the value returned from the database is not mapped onto the Java object.</p>
<p>i.e.</p>
<p>In DB: <strong>02-JAN-10 13:30:00</strong><br />
In Java Object after mapping: <strong>02-JAN-10 00:00:00</strong></p>
<p>The Ibatis ResultMapper, snippet, looks like this:</p>
<pre>
....
&lt;result property="savedDate" column="SAVED_DATE" javaType="java.util.Date" jdbcType="DATE"/&gt;
....
</pre>
<p><code><span id="more-160"></span></code></p>
<h4>The Solution</h4>
<p>1. Implement our old friend the Ibatis TypeHandlerCallback.</p>
<pre class="brush: plain; title: ; notranslate">
package foo.bar;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

public class CustomDateHandler implements TypeHandlerCallback {

    public Object getResult(ResultGetter getter) throws SQLException {
        final Object obj = getter.getTimestamp();
        return obj != null ? (Date) obj : null;
    }

    public void setParameter(ParameterSetter setter,Object value) throws SQLException {
        setter.setTimestamp(value != null ? new Timestamp(((Date)value).getTime()) : null);
    }

    public Object valueOf(String datetime) {
        return Timestamp.valueOf(datetime);
    }
}
</pre>
<p>2. Add Typehandlers and TypeAliases to Ibatis config</p>
<pre>
&lt;typeAlias alias="OracleDateHandler" type="foo.bar.CustomDateHandler"/&gt;
&lt;typeHandler callback="OracleDateHandler" jdbcType="DATETIME" javaType="date"/&gt;
</pre>
<p>3. Update resultMap to use new &#8220;type&#8221;</p>
<pre>
....
&lt;result property="savedDate" column="SAVED_DATE" javaType="java.util.Date" jdbcType="DATETIME"/&gt;
....
</pre>
<h4>Important Note!</h4>
<p>This solution deals with issues caused by certain versions of the Oracle JDBC drivers. I believe that the 11g drivers fix this issue. As ever YMMV&#8230;..</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/using-ibatis-typehandlers-to-fix-oracle-date-missing-time/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ibatis TypeHandlerCallback Enum Example</title>
		<link>http://lstierneyltd.com/blog/development/examples/ibatis-typehandlercallback-enum-example/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/ibatis-typehandlercallback-enum-example/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 11:29:53 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[ibatis]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=147</guid>
		<description><![CDATA[Problem You need Ibatis to perform custom processing before parameters are set on a Prepared Statement or after the results are retrieved from the result set. Maybe to convert Y/N to boolean, map results to custom objects etc Solution You probably want to look at implementing Ibatis&#8217; TypeHandlerCallBack, the API docs for which are here [...]]]></description>
			<content:encoded><![CDATA[<h4>Problem</h4>
<p>You need Ibatis to perform custom processing before parameters are set on a Prepared Statement or after the results are retrieved from the result set. Maybe to convert Y/N to boolean, map results to custom objects etc</p>
<h4>Solution</h4>
<p>You probably want to look at implementing Ibatis&#8217; TypeHandlerCallBack, the API docs for which are <a target="_blank" href="http://ibatis.apache.org/docs/java/user/com/ibatis/sqlmap/client/extensions/TypeHandlerCallback.html">here</a><br />
<code><span id="more-147"></span></code></p>
<h4>Example</h4>
<p>In this example I map a &#8220;prev&#8221; or &#8220;next&#8221; coming back from the DB (as &#8220;direction&#8221;) to the Enum described in <a href="http://lstierneyltd.com/blog/development/java-5-enum-example/" target="_blank" >this post</a>.</p>
<h4>Ibatis Config</h4>
<pre class="brush: plain; title: ; notranslate">
&lt;parameterMap id=&quot;findByCompanyIdParams&quot; class=&quot;map&quot;&gt;
    &lt;parameter mode=&quot;IN&quot; property=&quot;companyId&quot; javaType=&quot;java.lang.String&quot; jdbcType=&quot;VARCHAR&quot;/&gt;
    &lt;parameter mode=&quot;IN&quot; property=&quot;nextKeyId&quot; javaType=&quot;java.lang.String&quot; jdbcType=&quot;VARCHAR&quot;/&gt;
    &lt;parameter mode=&quot;IN&quot; property=&quot;maxOccurs&quot; javaType=&quot;java.lang.Integer&quot; jdbcType=&quot;NUMBER&quot;/&gt;
    &lt;parameter mode=&quot;IN&quot; property=&quot;direction&quot; javaType=&quot;OBJECT&quot;
        typeHandler=&quot;foo.bar.PagingCriteriaDirectionTypeHandlerCallback&quot; jdbcType=&quot;VARCHAR&quot;/&gt;
    &lt;parameter mode=&quot;OUT&quot; property=&quot;outCursor&quot; jdbcType=&quot;ORACLECURSOR&quot; javaType=&quot;java.sql.ResultSet&quot;/&gt;
&lt;/parameterMap&gt;
</pre>
<h4>Java code</h4>
<pre class="brush: plain; title: ; notranslate">
package foo.bar;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
import foo.bar.MyRuntimeException;

public class PagingCriteriaDirectionTypeHandlerCallback implements TypeHandlerCallback{

	public Object getResult(ResultGetter getter) throws SQLException {
		String s = getter.getString();
		if (PagingCriteriaDirection.NEXT.getDescription().equalsIgnoreCase(s)) {
			return PagingCriteriaDirection.NEXT;
		} else if (PagingCriteriaDirection.PREV.getDescription().equalsIgnoreCase(s)) {
			return PagingCriteriaDirection.PREV;
		} else {
			throw new SQLException(&quot;Unexpected value &quot; + s + &quot; found where &quot;
					+ PagingCriteriaDirection.PREV.getDescription() + &quot; or &quot; + PagingCriteriaDirection.NEXT.getDescription() + &quot; was expected.&quot;);
		}
	}

	public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
		PagingCriteriaDirection direction = (PagingCriteriaDirection) parameter;

		if (direction.equals(PagingCriteriaDirection.NEXT)) {
			setter.setString(PagingCriteriaDirection.NEXT.getDescription());
		}
		else {
			setter.setString(PagingCriteriaDirection.PREV.getDescription());
		}
	}

	public Object valueOf(String s) {
	    if (PagingCriteriaDirection.NEXT.getDescription().equalsIgnoreCase(s)) {
	        return PagingCriteriaDirection.NEXT;
	    }
	    else if (PagingCriteriaDirection.PREV.getDescription().equalsIgnoreCase(s)) {
	        return PagingCriteriaDirection.PREV;
	    }
	    else {
	    	throw new MyRuntimeException(&quot;Unexpected value &quot; + s + &quot; found where &quot;
					+ PagingCriteriaDirection.PREV.getDescription() + &quot; or &quot; + PagingCriteriaDirection.NEXT.getDescription() + &quot; was expected.&quot;);
	    }
	}

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/ibatis-typehandlercallback-enum-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

