<?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>Schauderhaft &#187; domain driven design</title>
	<atom:link href="http://blog.schauderhaft.de/tag/domain-driven-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 05 Feb 2012 20:46:36 +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>An Analysis of NonUniqueObjectException and LazyInitializationException in Hibernate</title>
		<link>http://blog.schauderhaft.de/2010/07/11/an-analysis-of-nonuniqueobjectexception-and-lazyinitializationexception-in-hibernate/</link>
		<comments>http://blog.schauderhaft.de/2010/07/11/an-analysis-of-nonuniqueobjectexception-and-lazyinitializationexception-in-hibernate/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 19:25:19 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[domain driven design]]></category>
		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=529</guid>
		<description><![CDATA[I consider Hibernate a great and useful tool. But it has some mean Exceptions lurking in the darker corners. Today I&#8217;d like to explore the ones from the title in a little more detail, including different approaches on how to avoid them. Let&#8217;s start with the well known LazyInitializationException. When you search for this one [...]]]></description>
			<content:encoded><![CDATA[<p>I consider Hibernate a great and useful tool. But it has some mean Exceptions lurking in the darker corners. Today I&#8217;d like to explore the ones from the title in a little more detail, including different approaches on how to avoid them.</p>
<p>Let&#8217;s start with the well known <tt>LazyInitializationException</tt>. When you search for this one in the Internet you end up with scenarios like this one:</p>
<p>You load <tt>A</tt>, close the session you  used and try to access a property of <tt>A</tt> which gets loaded lazily resulting in a <tt>LazyInitializationException</tt>. That one is well described, including strategies to avoid it, which basically consist of not closing the session to early.</p>
<p>But there are more hideous scenarios. Assume the classes Mom, Dad and Kid are mapped as one would expect, with references from Mom and Dad to Kid. Now consider the following piece of code:</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">Session session = openSession<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">session.<span class="me1">beginTransaction</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Mom mom = <span class="br0">&#40;</span>Mom<span class="br0">&#41;</span> session.<span class="me1">load</span><span class="br0">&#40;</span>Mom.<span class="kw2">class</span>, momId<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">Kid momsKid = mom.<span class="me1">getKid</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// dad comes by</span></div>
</li>
<li class="li1">
<div class="de1">Dad dad = <span class="br0">&#40;</span>Dad<span class="br0">&#41;</span> session.<span class="me1">load</span><span class="br0">&#40;</span>Dad.<span class="kw2">class</span>, dadId<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">Kid dadsKid = dad.<span class="me1">getKid</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// and leaves</span></div>
</li>
<li class="li1">
<div class="de1">session.<span class="me1">evict</span><span class="br0">&#40;</span>dad<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// alas he has taken kido with him</span></div>
</li>
<li class="li1">
<div class="de1">momsKid.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Depending on your exact Mapping you might get a <tt>LazyInitializationException</tt> in the last line, although the session is still open. The problem here is that when dad got evicted the kid got evicted as well. And since Mom has a reference to the exact same object, that object isn&#8217;t attached to any session anymore. In order to see this effect you&#8217;ll have to use mappings like this:</p>
<div class="geshi no java">
<div class="head">@ManyToOne(fetch = LAZY, cascade = CascadeType.ALL)</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Cascade<span class="br0">&#40;</span>org.<span class="me1">hibernate</span>.<span class="me1">annotations</span>.<span class="me1">CascadeType</span>.<span class="me1">ALL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> Kid kid<span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>The <tt>NonUniqueObjectException</tt> gets caused by what could be considered the reverse process. Examine the following piece of code:</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Dad left for another woman, I mean Session</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Dad dad = loadDad<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Session session = openSession<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; session.<span class="me1">beginTransaction</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Mom mom = <span class="br0">&#40;</span>Mom<span class="br0">&#41;</span> session.<span class="me1">load</span><span class="br0">&#40;</span>Mom.<span class="kw2">class</span>, momId<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>mom.<span class="me1">getKid</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Now dad wants to move back in</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; session.<span class="me1">saveOrUpdate</span><span class="br0">&#40;</span>dad<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>The first method call loads a <tt>Dad</tt> object including <tt>Kid</tt> from a different session. Then from the &#8216;main&#8217; session <tt>Mom</tt> gets loaded including kid. When we attach the <tt>Dad</tt> object to the session it tries to attach its version of the <tt>Kid</tt> as well, which collides with the <tt>Kid</tt> already present in the session.</p>
<p>So how can we avoid these kind of problem? I see several options:</p>
<ul>
<li>Don&#8217;t use Cascade. Of course this means much more effort on your side in many cases, since you have to carefully track what you need to attach or evict from a session.</li>
<li>Don&#8217;t use <tt>evict</tt> and don&#8217;t reattach existing instances to sessions. I guess this should work in many cases. But I have seen cases where this approach would have caused a large overhead, because reloading complex object graphs.</li>
<li>Carefully design your cascading boundaries. A look at the Aggregate Root from <a href="http://www.amazon.de/gp/product/0321125215?ie=UTF8&#038;tag=schauderhafte-21&#038;linkCode=as2&#038;camp=1638&#038;creative=19454&#038;creativeASIN=0321125215">DDD</a> might help here. An Aggregate Root is a single class which works as a gate keeper (did anybody say facade) to a cluster of classes related to the Aggregate Root. When all the Hibernate related actions go against that Aggregate Root, and cascade from there to the content and only the content of the Aggregate Root you should be safe. All entities outside an Aggregate Root have to use the Aggregate Root or Repositories to gain access. This needs some bookkeeping. But when you do it properly it might actually improve the over all design of your code. Please note that all this is a thought experiment on my side, and I can&#8217;t promise that it actually works as intended, but I think it should.</li>
</ul>
<p>If you want the complete source code for the example, you can download this <a href="/wp-content/uploads/2010/07/sources.zip">source.zip</a> file.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/07/11/an-analysis-of-nonuniqueobjectexception-and-lazyinitializationexception-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Das beste Buch über Software Design&#8230;</title>
		<link>http://blog.schauderhaft.de/2008/03/06/das-beste-buch-uber-software-design/</link>
		<comments>http://blog.schauderhaft.de/2008/03/06/das-beste-buch-uber-software-design/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 18:55:49 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[domain driven design]]></category>
		<category><![CDATA[reading]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/03/06/das-beste-buch-uber-software-design/</guid>
		<description><![CDATA[&#8230; das ich seit langem gelesen habe ist Domain-Driven Design: Tackling Complexity in the Heart of Software Entgegen der allgegenwärtigen Presens von Objekt Orientiertem Design in der Literatur und im Internet kommen in meiner Erfahrung diese Techniken oft nur im Infrastruktur Code zur Anwendung (GUI Frameworks, Web Frameworks, Datenbankzugriff &#8230;). Wenn es aber um das [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230; das ich seit langem gelesen habe ist <strong>Domain-Driven Design: Tackling Complexity in the Heart of Software</strong><br />
<a href="http://www.amazon.de/gp/product/0321125215?ie=UTF8&amp;tag=schauderhafte-21&amp;linkCode=as2&amp;camp=1638&amp;creative=6742&amp;creativeASIN=0321125215"><img src="/wp-content/uploads/2008/01/21olwrbseul_aa_sl160_.jpg" border="0" alt="" /></a><img style="border: medium none  ! important; margin: 0px ! important" src="http://www.assoc-amazon.de/e/ir?t=schauderhafte-21&amp;l=as2&amp;o=3&amp;a=0321125215" border="0" alt="" width="1" height="1" /></p>
<p>Entgegen der allgegenwärtigen Presens von Objekt Orientiertem Design in der Literatur und im Internet kommen in meiner Erfahrung diese Techniken oft nur im Infrastruktur Code zur Anwendung (GUI Frameworks, Web Frameworks, Datenbankzugriff &#8230;). Wenn es aber um das eigentliche Problem geht, ist die Basis auf einmal das Datenmodell. Ich weiß um den Wert einer guten Datenbank. Aber die Möglichkeiten Geschäftslogik in der Datenbank sichtbar zu machen sind extrem beschränkt. Es gibt nicht viel mehr als Foreign Keys, um Beziehungen zwischen Daten zu repräsentieren. Es ist also völlig ungeeignet das Domänen Modell, dass jeder Anwendung zu Grunde liegt wiederzuspiegeln. Wenn es aber nicht im Sourcecode ist, wo ist es dann? Gemäß Agilen Methoden: Nirgends, jedenfalls nicht in schriftlicher Form. Und gemäß Wasserfall oder V-Modell oder dergleichen? In einem veralteten Dokument!</p>
<p>Martin Fowler schlägt als anwendbares Enterprise Pattern das <a href="http://martinfowler.com/eaaCatalog/domainModel.html">Domainmodel</a> vor, das nach allen Regeln der objektorientierten Kunst erstellt wird. Evans liefert in seinem Buch nun das Handwerkszeug, um mit diesem Domänen Model möglichst produktiv zu arbeiten. Er beschreibt wie Refactoring, Patterns, Modularisierung und vieles mehr in diesem Kontext eingesetzt werden können um das Domänen Model im Source Code ausdrucksstark zu halten und wie es als Grundlage für eine gemeinsame Sprache (Ubiquitous Language) zur Basis sämtlicher Arbeit im Projekt wird. Dies alleine wäre schon Grund genug das Buch zur Pflicht für jeden Softwaredesigner und Architekten zu machen. Hinzu kommt, dass es sich sehr angenehm liest, unter anderem da er realitätsnahe Beispiele verwendet und Beispiele aus der eigenen Erfahrung bringt die zeigen was nicht funktioniert.</p>
<p>In manchen Reviews werden ihm Wiederholungen vorgeworfen. Einiges was so mancher für Wiederholungen hält, sind keine, sondern nur ähnliche oder verwandte Konzepte. Vieles schreibt aber tatsächlich mehrfach, allerdings in einem Umfang, den ich als sehr angenehm empfand.</p>
<p align="center">Äußerst empfehlenswert.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2008/03/06/das-beste-buch-uber-software-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

