<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Richard Bolkey - Blog</title>
	
	<link>http://blog.bolkey.com</link>
	<description>Thoughts of a plain old java developer.</description>
	<lastBuildDate>Mon, 30 Nov 2009 02:51:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/rbolkey" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="rbolkey" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Building a contextual recursive tree in Tapestry 5</title>
		<link>http://blog.bolkey.com/2009/06/tapestry-5-recursive-tree/</link>
		<comments>http://blog.bolkey.com/2009/06/tapestry-5-recursive-tree/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 21:09:15 +0000</pubDate>
		<dc:creator>rbolkey</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tapestry]]></category>

		<guid isPermaLink="false">http://blog.bolkey.com/?p=29</guid>
		<description><![CDATA[UPDATE: There have been a few requests for a more complete source code example.  The following link is to a demo application of the concepts from this entry, and the source code in this entry has been updated to reflect the content of the demo. Recursive Tapestry Tree Demo Application
We use Tapestry 5 at [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.bolkey.com%2F2009%2F06%2Ftapestry-5-recursive-tree%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.bolkey.com%2F2009%2F06%2Ftapestry-5-recursive-tree%2F" height="61" width="51" /></a></div><p><strong>UPDATE</strong>: There have been a few requests for a more complete source code example.  The following link is to a demo application of the concepts from this entry, and the source code in this entry has been updated to reflect the content of the demo. <a href='http://blog.bolkey.com/2009/06/tapestry-5-recursive-tree/tapestry-tree/' rel='attachment wp-att-53'>Recursive Tapestry Tree Demo Application</a></p>
<p>We use Tapestry 5 at work.  Tapestry is a wonderful, efficient, and powerful framework, but like any piece of technology, there are headaches.  The headache we had this past week was how on earth to render a tree of of components where we have to dynamically select the component depending upon the current context within the tree.  Here&#8217;s the approach we uncovered using three components: a component acting as a container for the tree, another to render individual nodes of the tree (contextually subclassed for different nodes), and a custom delegate component (to turn off parameter caching).</p>
<h2>The Container Component</h2>
<p>The container component maintains stateful tree information while rendering the tree, and selects the component to render for the current node of the tree.</p>
<p>Here is the container&#8217;s template.</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;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">xmlns:t</span>=<span style="color: #ff0000;">&quot;http://tapestry.apache.org/schema/tapestry_5_1_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The custom delegate to the component to render based on the context of the current node. --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:nodedelegate</span> <span style="color: #000066;">to</span>=<span style="color: #ff0000;">&quot;currentNodeRenderer&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:block<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- This block contains a set of components to delegate rendering to based on the current node. --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">t:id</span>=<span style="color: #ff0000;">&quot;blackNode&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- Delegate to the child component to render.  This replaces the component's body. --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:nodedelegate</span> <span style="color: #000066;">to</span>=<span style="color: #ff0000;">&quot;currentNodeRenderer&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">t:id</span>=<span style="color: #ff0000;">&quot;redNode&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:nodeDelegate</span> <span style="color: #000066;">to</span>=<span style="color: #ff0000;">&quot;currentNodeRenderer&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/t:block<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><span id="more-29"></span><br />
And the container&#8217;s implementation.</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> Tree <span style="color: #009900;">&#123;</span>
&nbsp;
    @Parameter<span style="color: #009900;">&#40;</span>required <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Node node<span style="color: #339933;">;</span>
&nbsp;
    @<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>id <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;blackNode&quot;</span>, parameters <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;node=currentNode&quot;</span>, <span style="color: #0000ff;">&quot;childPosition=currentChildPosition&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> RenderBlackNode blackNode<span style="color: #339933;">;</span>
&nbsp;
    @<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>id <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;redNode&quot;</span>, parameters <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;node=currentNode&quot;</span>, <span style="color: #0000ff;">&quot;childPosition=currentChildPosition&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> RenderRedNode redNode<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Node currentNode<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>Node, Integer<span style="color: #339933;">&gt;</span> childPositions<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Node getCurrentNode<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;">return</span> currentNode<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setCurrentNode<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Node node<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// set the initial child position for the node to 0 if it has not been seen yet.</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>childPositions.<span style="color: #006633;">containsKey</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            childPositions.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>node, <span style="color: #cc66cc;">0</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;">this</span>.<span style="color: #006633;">currentNode</span> <span style="color: #339933;">=</span> node<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getCurrentChildPosition<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;">return</span> childPositions.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">currentNode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setCurrentChildPosition<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> pos<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">childPositions</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>currentNode, pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Selects the Node component to render based on the currentNode type.
     *
     * @return the component to render the current node
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> BaseRenderNode getCurrentNodeRenderer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        BaseRenderNode object <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentNode <span style="color: #000000; font-weight: bold;">instanceof</span> BlackNode<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            object <span style="color: #339933;">=</span> blackNode<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentNode <span style="color: #000000; font-weight: bold;">instanceof</span> RedNode<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            object <span style="color: #339933;">=</span> redNode<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> object<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">void</span> setupRender<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        childPositions <span style="color: #339933;">=</span> CollectionFactory.<span style="color: #006633;">newMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setCurrentNode<span style="color: #009900;">&#40;</span>node<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>

<h2>The Custom Delegate</h2>
<p>This was the <strong>crucial</strong> component that unlocked our ability to build the tree.  As you can see above, the delegate will be encountered with every node in the tree, and since the type of a child node may change, the delegate needs to be able to change the component that it references.  The built-in Delegate component in Tapestry 5 cannot do this, since the &#8220;to&#8221; parameter is cached.  We needed to build a custom Delegate component identical to the built-in one but with the &#8220;to&#8221; parameter uncached so that the delegate would query the container for what component to render each time the delegate is encountered.</p>
<h2>The Node Component (with subclasses)</h2>
<p>Each type of node in the tree needs its own component type, but they all extend from an identical base component that is responsible for changing the current node to render as well as updating the index of the next child node to render.</p>
<p>Each subcomponent needs to share this basic structure:</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;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">xmlns:t</span>=<span style="color: #ff0000;">&quot;http://tapestry.apache.org/schema/tapestry_5_1_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- The body is replaced with component(s) for the child nodes. --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;t:body</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here&#8217;s the base Node component.</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> BaseRenderNode <span style="color: #009900;">&#123;</span>
&nbsp;
    @Parameter<span style="color: #009900;">&#40;</span>required <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span>, cache <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Node node<span style="color: #339933;">;</span>
&nbsp;
    @Parameter
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> childPosition<span style="color: #339933;">;</span>
&nbsp;
    @SetupRender
    <span style="color: #000066; font-weight: bold;">boolean</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// prevents rending with the node parameter is null.</span>
        <span style="color: #000000; font-weight: bold;">return</span> node <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @BeforeRenderBody
    <span style="color: #000066; font-weight: bold;">boolean</span> beforeChild<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// the node  has children, render the body to render a child</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">boolean</span> render <span style="color: #339933;">=</span> node.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>render<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// sets the container's currentNode to the node's child at the given index.</span>
            node <span style="color: #339933;">=</span> node.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>childPosition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> render<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @AfterRenderBody
    <span style="color: #000066; font-weight: bold;">boolean</span> afterChild<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// increment the child position, afterRender on the child will have the container's currentNode set back to the node before the body was rendered.</span>
        childPosition <span style="color: #339933;">=</span> childPosition <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// return true on last child index, finishing the iteration over the children, otherwise re-render the body (to render the next child)</span>
        <span style="color: #000000; font-weight: bold;">return</span> node.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> childPosition<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @AfterRender
    <span style="color: #000066; font-weight: bold;">void</span> after<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// set the currentNode to the parent after render (pop the stack)</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>node.<span style="color: #006633;">isRoot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            node <span style="color: #339933;">=</span> node.<span style="color: #006633;">getParent</span><span style="color: #009900;">&#40;</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>

<h2>Conclusion</h2>
<p>We have seen how a tree-like structure can be rendered through Tapestry 5.  Tapestry&#8217;s ability to dynamically render static templates made this possible, but we needed to turn off a caching optimization in the Delegate component in order to increase the dynamism.</p>
<img src="http://feeds.feedburner.com/~r/rbolkey/~4/rwx50JolHGM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bolkey.com/2009/06/tapestry-5-recursive-tree/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The Trouble with Proxies: Hibernate or DataNucleus</title>
		<link>http://blog.bolkey.com/2009/05/hibernate-datanucleus-r1/</link>
		<comments>http://blog.bolkey.com/2009/05/hibernate-datanucleus-r1/#comments</comments>
		<pubDate>Fri, 22 May 2009 22:39:39 +0000</pubDate>
		<dc:creator>rbolkey</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Datanucleus]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Persistence]]></category>

		<guid isPermaLink="false">http://blog.bolkey.com/?p=21</guid>
		<description><![CDATA[Hibernate and DataNucleus (JDO2) are arguably the two most substantial persistence frameworks in the open source ecosystem.  I have worked with DataNucleus quite a bit on the side, and I am now finding an opportunity to use Hibernate during my day job.
I have now spent enough time using Hibernate to appreciate the approach that DataNucleus [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.bolkey.com%2F2009%2F05%2Fhibernate-datanucleus-r1%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.bolkey.com%2F2009%2F05%2Fhibernate-datanucleus-r1%2F" height="61" width="51" /></a></div><p><a href="http://www.hibernate.org/">Hibernate</a> and <a href="http://www.datanucleus.org/">DataNucleus</a> (JDO2) are arguably the two most substantial persistence frameworks in the open source ecosystem.  I have worked with DataNucleus quite a bit on the side, and I am now finding an opportunity to use Hibernate during my day job.</p>
<p>I have now spent enough time using Hibernate to appreciate the approach that DataNucleus takes using bytecode enhancement in comparison to Hibernate&#8217;s proxies.  Essentially, I am finding myself having to appease Hibernate&#8217;s view of the world in unexpected ways where I would like to leave my code alone. Two problem cases have come up so far that I would not have encountered with DataNucleus: using <code>instanceof</code> with type hierarchies and implementing <code>equals</code>.  In both of these case, bytecode enhancement would leave my Java code to work just as I expected; but instead, I have to deal with an extra layer of indirection where I was expecting to deal directly with my objects.</p>
<h2>Type hierarchies and <code>instanceof</code></h2>
<p>Lets say that I have the following type hierarchy:</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;">interface</span> Transformer <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Autobot <span style="color: #000000; font-weight: bold;">extends</span> Transformer <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Decepticon <span style="color: #000000; font-weight: bold;">extends</span> Transformer <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Obviously, there are times that I may be given an instance of type Transformer where I want to distinguish between an Autobot and a Decepticon using <code>instanceof</code>.  However, if I am persisting these instances with Hibernate, and I receive a proxy to the persisted entity, I cannot use <code>instanceof</code> to determine the type of the instance I am dealing with.  In Hibernate, the entity instance could well be an Autobot, but the returned proxy to the entity will implement both the Autobot and the Decepticon types.</p>
<p>In order to determine the instance type I am dealing with, I have to either unwrap the proxy or turn off lazy loading.  Both of these are undesirable solutions.  Unwrapping the proxy adds extra persistence operations outside of the persistence tier of your application, which violates a clean separation of concerns.  And of course, there is a performance hit for turning off lazy loading.  I wouldn&#8217;t have to use either of these undesirable solutions with DataNucleus.<br />
<span id="more-21"></span></p>
<h2>Implementing <code>equals</code></h2>
<p>This problem seems well documented within the Hibernate community, but it is still an annoyance and evidence of how Hibernate&#8217;s design forces you to alter expected Java constructs to fit Hibernate&#8217;s needs.  If you want an <code>equals</code> method to play friendly with Hibernate proxies you need to.</p>
<ul>
<li><strong>Do comparisons on properties instead of fields</strong>. If you compare on fields, the proxy may return null, comparing on properties will initialize the proxy and return the expected result.</li>
<li><strong>Use <code>instanceof</code> comparison instead of class equality</strong>.  The type of the proxy is not equal to the type of the entity, but rather extends the type of the entity.</li>
<li><strong>Compare to the interface instead of the implementing class</strong>. You need to do this if you are using interface proxies instead.</li>
</ul>
<p>All of the above break the expected construct of the <code>equals</code> method, and DataNucleus does not force you to make any of these concessions.</p>
<h2>Conclusion</h2>
<p>Obviously, I&#8217;m still expanding my knowledge in the Hibernate way of the world, but from just these two headaches I have had with Hibernate, DataNucleus&#8217; approach of using bytecode enhancement looks much superior to the proxying that Hibernate does.</p>
<img src="http://feeds.feedburner.com/~r/rbolkey/~4/DydNPkjOxbM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bolkey.com/2009/05/hibernate-datanucleus-r1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 2.699 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-02-06 04:58:25 -->
