<?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>info.michael-simons.eu &#187; Hibernate</title>
	<atom:link href="http://info.michael-simons.eu/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://info.michael-simons.eu</link>
	<description>Just another nerd blog</description>
	<lastBuildDate>Wed, 25 Jan 2012 07:53:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Fixing hibernate &#8220;Cannot release connection&#8221; exception using DBCP and MySQL.</title>
		<link>http://info.michael-simons.eu/2011/11/21/fixing-hibernate-cannot-release-connection-exception-using-dbcp-and-mysql/</link>
		<comments>http://info.michael-simons.eu/2011/11/21/fixing-hibernate-cannot-release-connection-exception-using-dbcp-and-mysql/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 08:04:34 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[DBCP]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=550</guid>
		<description><![CDATA[Every 8 hours i got a Hibernate exception &#8220;Cannot release connection&#8221; within a Java application using Hibernate, Apache DBCP on Tomcat: org.hibernate.exception.GenericJDBCException: Cannot release connection at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29) .. .. Caused by: java.sql.SQLException: Already closed. Not only that the messages polluted my inbox, the exception was visible to the enduser, [...]]]></description>
			<content:encoded><![CDATA[<p>Every 8 hours i got a Hibernate exception &#8220;Cannot release connection&#8221; within a Java application using Hibernate, Apache DBCP on Tomcat:</p>
<pre>
org.hibernate.exception.GenericJDBCException: Cannot release connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
    ..
    ..
Caused by: java.sql.SQLException: Already closed.
</pre>
<p>Not only that the messages polluted my inbox, the exception was visible to the enduser, resulting in a HTTP 500 error. An older <a href="http://mrather.blogspot.com/2008/09/hibernate-and-connection-pools.html">blog post</a> i found suggested dismissing DBCP and using c3p0, a solution that i&#8217;m not quite found of. At least, the post helped to reproduce the problem within my development setup. The underlying problem was indeed the MySQL wait_timeout.</p>
<p>There&#8217;s quite a long documentation on the <a href="http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html">Tomcat JDBC Connection Pool</a>. Although the Tomcat team recommends their own solution since Tomcat 7, i still wanted to go with DBCP.</p>
<p>The relevant keywords are &#8220;testOnBorrow&#8221;, &#8220;testOnReturn&#8221;, &#8220;testWhileIdle&#8221;, &#8220;validationQuery&#8221; and &#8220;timeBetweenEvictionRunsMillis&#8221;. The first 3 are boolean values. If set to true, the query given as validationQuery is executed on borrowing a connection from the pool, on returning or when idling. The first option is not an option on production use as the query is executed before <strong>each</strong> call. Although &#8220;Select 1&#8243; is probably very fast, i just don&#8217;t want to have. Also: The problem is an invalidated, idle connection so i set testWhileIdle to true. And what happened? Nothing! The problem stayed. So there is the last option timeBetweenEvictionRunsMillis which should, according to the docs, default to 5 seconds but it doesn&#8217;t. The documentation is <strong>wrong</strong>. It&#8217;s under zero, so the eviction thread that tests idle connections never run. I&#8217;ve tweeted the tomcat team, but there was no reaction. </p>
<p>So the correct configuration for a DBCP pool database source is:</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;Resource</span></span>
<span style="color: #009900;">	<span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;javax.sql.DataSource&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">driverClassName</span>=<span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">maxActive</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">maxIdle</span>=<span style="color: #ff0000;">&quot;30&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">maxWait</span>=<span style="color: #ff0000;">&quot;10000&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">testOnBorrow</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">testOnReturn</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">testWhileIdle</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">validationQuery</span>=<span style="color: #ff0000;">&quot;Select 1&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">timeBetweenEvictionRunsMillis</span>=<span style="color: #ff0000;">&quot;1800000&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>This way the eviction thread runs every 30 minutes, testing idle connections with the query &#8220;Select 1&#8243; and removing them from the pool. The timeBetweenEvictionRunsMillis should not be to low. It should be adapted to the configured MySQL wait_timeout.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=550&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_550" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/11/21/fixing-hibernate-cannot-release-connection-exception-using-dbcp-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some random Grails thoughts and tipps</title>
		<link>http://info.michael-simons.eu/2008/12/04/some-random-grails-thoughts-and-tipps/</link>
		<comments>http://info.michael-simons.eu/2008/12/04/some-random-grails-thoughts-and-tipps/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 10:46:33 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=236</guid>
		<description><![CDATA[Lately i&#8217;ve been rambling and ranting a lot on twitter about the Grails framework. To my surprise, many other developers actually read this tweets and helped me out on some problems. Thanks a lot gals and guys, i really appreciate that. Me rambling isn&#8217;t meant to be personal at any time, i guess you know [...]]]></description>
			<content:encoded><![CDATA[<p>Lately i&#8217;ve been rambling and ranting a lot on twitter about the <a href="http://grails.org/">Grails</a> framework.</p>
<p>To my surprise, many other developers actually read this tweets and helped me out on some problems. Thanks a lot gals and guys, i really appreciate that. Me rambling isn&#8217;t meant to be personal at any time, i guess you know how easily one gets frustrated with too less time and too much stuff to do.</p>
<p>Anyway, here are some shortcuts that could eventually be helpful. I&#8217;m gonna add more to this list the next days:</p>
<h3>Enabling hibernate filters in the grails session</h3>
<p>Took me a little digging through the source code, but i came up with the following idea:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.springframework.transaction.support.TransactionSynchronizationManager</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SecurityFilters <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> sessionFactory
  <span style="color: #000000; font-weight: bold;">def</span> filters <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
    login<span style="color: #66cc66;">&#40;</span>controller:<span style="color: #ff0000;">'*'</span>, action:<span style="color: #ff0000;">'*'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      before <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// get your user id somewhere</span>
        <span style="color: #000000; font-weight: bold;">def</span> whatsoeveruserId <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
        <span style="color: #000000; font-weight: bold;">def</span> sessionHolder <span style="color: #66cc66;">=</span>  TransactionSynchronizationManager.<span style="color: #006600;">getResource</span><span style="color: #66cc66;">&#40;</span>sessionFactory<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>     
        sessionHolder.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">enableFilter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;filterByOwner&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;currentUserId&quot;</span>, whatsoeveruserId<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I want to have some kind of rowlevel security through a Hibernate filter. Through dependency injection i get hold of the sessionFactory and through the TA Manager, i get the current session on which i can enable my filter.</p>
<p>Doing this in a Grails filter, i can combine this with some kinda login mechanism and i&#8217;m good to go.</p>
<h3>Whitelisting attributes through bindData</h3>
<p>To me it&#8217;s a bad idea using blacklisting on data binding as i can and will forget attributes that must not be updated through a webform.</p>
<p>With <a href="http://www.anyware.co.uk/2005/">Marc</a> i found the following solution:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">bindData<span style="color: #66cc66;">&#40;</span>entity, params, entity.<span style="color: #006600;">properties</span>.<span style="color: #663399;">collect</span><span style="color: #66cc66;">&#123;</span>it.<span style="color: #006600;">key</span><span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">-</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'foo'</span>, <span style="color: #ff0000;">'bar'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>That way only attributes foo and bar gets updated.</p>
<p>Anyway, with Grails 1.1 this won&#8217;t be necessary anymore as Graeme <a href="http://graemerocher.blogspot.com/2008/10/new-gorm-features-coming-in-11.html">anonced</a>.</p>
<p><ins datetime="2008-12-04T12:55:33+00:00">Graeme was so kind to comment on this: This feature is already in 1.0.x, i just didn&#8217;t find it, have a look at the docu at <a href="http://grails.org/doc/1.1/guide/6.%20The%20Web%20Layer.html#6.1.6%20Data%20Binding">The Web Layer</a>.</ins></p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">bindData<span style="color: #66cc66;">&#40;</span>entity, params,  <span style="color: #66cc66;">&#91;</span>include:<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'foo'</span>, <span style="color: #ff0000;">'bar'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p><ins datetime="2008-12-09T12:31:41+00:00">Updates on 2008/12/9</ins></p>
<h3>Adding custom errors to a domain class</h3>
<p>The grails reference has a handy example for adding custom errors to domain classes, have a look <a href="http://grails.org/doc/1.0.x/ref/Domain%20Classes/errors.html">here</a>. This works quite well except that all other errors from databinding are mysteriously gone. </p>
<p>For me, the following steps worked to update a user (change some persistent attributes and the transient attributes password and passwordConfirmation):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">bindData<span style="color: #66cc66;">&#40;</span>anwender, params, <span style="color: #66cc66;">&#91;</span>include:<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span>, <span style="color: #ff0000;">'vorname'</span>, <span style="color: #ff0000;">'password'</span>, <span style="color: #ff0000;">'passwordConfirmation'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">password</span> <span style="color: #66cc66;">!=</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> params.<span style="color: #006600;">password</span> <span style="color: #66cc66;">==</span> params.<span style="color: #006600;">passwordConfirmation</span><span style="color: #66cc66;">&#41;</span>
  anwender.<span style="color: #006600;">hashPassword</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// As alway, never ever store plaintext passwords ;)</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">password</span> <span style="color: #66cc66;">!=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  anwender.<span style="color: #006600;">validate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// IMPORTANT without that step, possible other errors from bindData vanished</span>
  anwender.<span style="color: #006600;">errors</span>.<span style="color: #006600;">rejectValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'password'</span>, <span style="color: #ff0000;">'user.anwender.passwords_doesnotmatch'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Afterwords, hasErrors() show all errors, i.a. non nullable fields and the like.</p>
<h3>More thoughts</h3>
<p>I somewhat used to hibernate and come along very well with it, even though i&#8217;m actually a SQL fan. I guess if my inside into the Spring Framework would be a little bit deeper, some areas wouldn&#8217;t be hard to understand.</p>
<p>On the other hand i think that Grails does a great job for J2EE based development and it should do so even more. As always, there is the <a href="http://info.michael-simons.eu/2006/04/22/the-law-of-leaky-abstractions/">law of leaky abstractions</a>, but the whole butload of stuff that is the J2EE stack should be abstracted away.</p>
<p><ins datetime="2009-02-06T09:00:00+00:00">Updates on 2009/2/6</ins></p>
<h3>Grails 1.1-beta3</h3>
<p>I use hibernate validator in my domain classes (that i created outside of rails as hibernate annotated classes) and i got</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">NoSuchMethodError</span><span style="color: #339933;">:</span> org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">event</span>.<span style="color: #006633;">PreInsertEvent</span>.<span style="color: #006633;">getSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>Lorg<span style="color: #339933;">/</span>hibernate<span style="color: #339933;">/</span>engine<span style="color: #339933;">/</span>SessionImplementor</pre></div></div>

<p>on every insert and update. Hibernate validator 3.0.0.GA is incompatible with the Hibernate version in Grails 1.1-beta3. Problem was gone after upgrading validator to 3.1.0.GA.</p>
<p>Some other stuff:</p>
<ul>
<li>
Installed plugins are obviously gone. i.e yui plugin is still in the application folder, it needs to be reinstalled after upgrade (<i>grails install-plugin yui</i>)</p>
<p>Ok, i see  this was done on purpose: &#8220;Plugins are now stored in your USER_HOME directory. You will need to re-install your plugins or run&#8221; (from the beta2 release note). Not a good decision making this a default imho. I like having my apps pinned to specific plugins.
</li>
<li>The message method for doing I18n in controllers used to be available in filters. This method seems to be gone. No solution for that so far.</li>
<li><del>Values not bound in a form are not null anymore but 0 in case of numeric values. Bummer!</del> Actually my bad.</li>
<li><a href="http://info.michael-simons.eu/2009/02/12/grails-hibernate-current-session-context/">Some problems solved.</li>
</ul>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=236&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_236" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2008/12/04/some-random-grails-thoughts-and-tipps/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Hibernate and Inheritance Mapping</title>
		<link>http://info.michael-simons.eu/2008/05/27/hibernate-and-inheritance-mapping/</link>
		<comments>http://info.michael-simons.eu/2008/05/27/hibernate-and-inheritance-mapping/#comments</comments>
		<pubDate>Tue, 27 May 2008 12:59:57 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Shortcuts]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Tipps]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=169</guid>
		<description><![CDATA[Hibernate supports multiple types of inheritance mapping, some of them (Table per class, Table per subclass) using a discriminator column to decide the class. The type of the discriminator can be one of string, character, integer, byte, short, boolean, yes_no, true_false In case you need to use any other than string or character, i.e. integer, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.hibernate.org">Hibernate</a> supports multiple types of inheritance mapping, some of them (<a href="http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html#inheritance-tableperclass">Table per class</a>, <a href="http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html#inheritance-tablepersubclass-discriminator">Table per subclass</a>) using a discriminator column to decide the class.</p>
<p>The type of the discriminator can be one of</p>
<pre>
string, character, integer, byte, short, boolean, yes_no, true_false
</pre>
<p>In case you need to use any other than string or character, i.e. integer, you have to give the base class a default discriminator-value like 0 or -1 or whatever fits, otherwise you&#8217;ll end up with a an exception like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">MappingException</span><span style="color: #339933;">:</span> Could not format discriminator value to SQL string</pre></div></div>

<p>as Hibernate uses the class name of the base class to derive a default value.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=169&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_169" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2008/05/27/hibernate-and-inheritance-mapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Hibernate with Oracle Spatial</title>
		<link>http://info.michael-simons.eu/2007/02/05/using-hibernate-with-oracle-spatial/</link>
		<comments>http://info.michael-simons.eu/2007/02/05/using-hibernate-with-oracle-spatial/#comments</comments>
		<pubDate>Mon, 05 Feb 2007 12:18:40 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Oracle Spatial]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/2007/02/05/using-hibernate-with-oracle-spatial/</guid>
		<description><![CDATA[Oracle Spatial Datatype JGeometry from the spatial api (which can be found here) can easily be used with hibernate through a custom dialect and a custom type that delegates to an instance from oracle sdo api. Everything that is needed is here: Mapping Spatial Oracle type SDO_GEOMETRY to JGeometry The only quirk with this solution [...]]]></description>
			<content:encoded><![CDATA[<p>Oracle Spatial Datatype JGeometry from the spatial api (which can be found <a href="http://www.oracle.com/technology/software/products/spatial/index.html">here</a>) can easily be used with hibernate through a custom dialect and a custom type that delegates to an instance from oracle sdo api. Everything that is needed is here:</p>
<p><a href="http://www.hibernate.org/402.html">Mapping Spatial Oracle type SDO_GEOMETRY to JGeometry</a></p>
<p>The only quirk with this solution as of February 2007 is the fact, it won&#8217;t work with null columns. The fault lies in nullSafeSet. Either can the JGeometry delegate be null or, what is worse,  preparedStatement.setNull( i, Types.OTHER); will fail with an invalid column type. The correct version of this method is as follows:</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: #000066; font-weight: bold;">void</span> nullSafeSet<span style="color: #009900;">&#40;</span> <span style="color: #003399;">PreparedStatement</span> preparedStatement, <span style="color: #003399;">Object</span> o, <span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    preparedStatement.<span style="color: #006633;">setNull</span><span style="color: #009900;">&#40;</span>i, <span style="color: #003399;">Types</span>.<span style="color: #006633;">STRUCT</span>, <span style="color: #0000ff;">&quot;MDSYS.SDO_GEOMETRY&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> o <span style="color: #000000; font-weight: bold;">instanceof</span> JGeometryType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      JGeometryType gt <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JGeometryType<span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
      OracleConnection oc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>OracleConnection<span style="color: #009900;">&#41;</span> preparedStatement.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMetaData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>gt.<span style="color: #006633;">getJGeometry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        preparedStatement.<span style="color: #006633;">setNull</span><span style="color: #009900;">&#40;</span>i, <span style="color: #003399;">Types</span>.<span style="color: #006633;">STRUCT</span>, <span style="color: #0000ff;">&quot;MDSYS.SDO_GEOMETRY&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">else</span>
        preparedStatement.<span style="color: #006633;">setObject</span><span style="color: #009900;">&#40;</span> i, JGeometry.<span style="color: #006633;">store</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>JGeometry<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>gt<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getJGeometry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, oc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Furthermore, i think the spatial dialect should be registered with the following class:</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> OracleSpatialDialect <span style="color: #000000; font-weight: bold;">extends</span> Oracle9Dialect <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> OracleSpatialDialect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      registerColumnType<span style="color: #009900;">&#40;</span> <span style="color: #003399;">Types</span>.<span style="color: #006633;">OTHER</span>, <span style="color: #0000ff;">&quot;MDSYS.SDO_GEOMETRY&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Kudos to Joel Schuster from Navisys for the adapter class!</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=57&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_57" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2007/02/05/using-hibernate-with-oracle-spatial/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 3.742 seconds -->

