<?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>Dan Lynn&#187; groovy Archives  &#8211; Dan Lynn</title>
	<atom:link href="http://danlynn.com/tag/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://danlynn.com</link>
	<description>Finding adventure in just about everything</description>
	<lastBuildDate>Thu, 17 Jun 2010 16:47:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Groovy deserialization troubles</title>
		<link>http://danlynn.com/general/groovy-deserialization-troubles/</link>
		<comments>http://danlynn.com/general/groovy-deserialization-troubles/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 18:06:47 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[deserialization]]></category>
		<category><![CDATA[deserialize]]></category>
		<category><![CDATA[gorm]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[serialize]]></category>

		<guid isPermaLink="false">http://danlynn.com/?p=48</guid>
		<description><![CDATA[In a grails application, I needed to persist a complicated expression tree to the database, but I didn&#8217;t want to have Hibernate generate a database table for the information as it would greatly affect read performance (lots of joins) and I don&#8217;t need to have relational access to subsets of the tree. I decided to [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.grails.org/" target="_blank">grails</a> application, I needed to persist a complicated expression tree to the database, but I didn&#8217;t want to have Hibernate generate a database table for the information as it would greatly affect read performance (lots of joins) and I don&#8217;t need to have relational access to subsets of the tree. I decided to serialize the entire tree into a BLOB.</p>
<p>After creating a <a href="https://www.hibernate.org/73.html" target="_blank">Hibernate userType</a> to handle the serialization/deserialization, I ran into a ClassNotFoundException when attempting to deserialize my object.  I found out that this is due to java using the &#8220;last defined ClassLoader&#8221; when deserializing with an ObjectInputStream, which might not be the right ClassLoader in a Groovy environment (see: <a href="http://jira.codehaus.org/browse/GROOVY-1627" target="_blank">http://jira.codehaus.org/browse/GROOVY-1627</a>.</p>
<p>The solution? <strong>Subclass ObjectInputStream</strong> to allow you to pass in a predefined ClassLoader and override the resolveClass(ObjectStream classDesc) to use this ClassLoader parameter:</p>
<pre class="brush: java;">

public class ClassLoaderAwareObjectInputStream extends ObjectInputStream {

 private ClassLoader myClassLoader;

 public ClassLoaderAwareObjectInputStream(ClassLoader myClassLoader) throws IOException, SecurityException {
 super();
 this.myClassLoader = myClassLoader;
 }

 public ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader myClassLoader) throws IOException {
 super(in);
 this.myClassLoader = myClassLoader;
 }

 @Override
 protected Class resolveClass(ObjectStreamClass desc) throws IOException,
 ClassNotFoundException {
 String name = desc.getName();
 return Class.forName(name, false, myClassLoader);
 }
}
</pre>
<p>Thanks to <a href="http://satish.name/?p=66" target="_blank">Satish Gunnu</a> for the tip.</p>
]]></content:encoded>
			<wfw:commentRss>http://danlynn.com/general/groovy-deserialization-troubles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
