<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: JPA implementation patterns: Retrieving entities</title>
	<atom:link href="http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/</link>
	<description></description>
	<lastBuildDate>Thu, 18 Mar 2010 13:27:36 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: JPA Implementation Patterns &#124; Upthrust</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-94020</link>
		<dc:creator>JPA Implementation Patterns &#124; Upthrust</dc:creator>
		<pubDate>Sat, 30 Jan 2010 11:29:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-94020</guid>
		<description>[...] Retrieving Entities [...]</description>
		<content:encoded><![CDATA[<p>[...] Retrieving Entities [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Kovacs</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-92261</link>
		<dc:creator>Peter Kovacs</dc:creator>
		<pubDate>Sat, 08 Aug 2009 13:40:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-92261</guid>
		<description>@Dave: &quot;implementing a Specification to return a Criteria object&quot;

But this wouldn&#039;t be Specifiation pattern any more, would it be? Based on the article referenced by Lars, in the center of the Specification pattern there is an object which decides whether a domain object satisfies some condition(s) or not. In what you suggest, the decision is made outside the Specification, so I am not sure if this is still the same pattern. (I assume that by &quot;Criteria object&quot;, you mean a QueryDefinition object.)</description>
		<content:encoded><![CDATA[<p>@Dave: &#8220;implementing a Specification to return a Criteria object&#8221;</p>
<p>But this wouldn&#8217;t be Specifiation pattern any more, would it be? Based on the article referenced by Lars, in the center of the Specification pattern there is an object which decides whether a domain object satisfies some condition(s) or not. In what you suggest, the decision is made outside the Specification, so I am not sure if this is still the same pattern. (I assume that by &#8220;Criteria object&#8221;, you mean a QueryDefinition object.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JPA Implementation Patterns &#171; Fernando Franzini Java Blog</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-92098</link>
		<dc:creator>JPA Implementation Patterns &#171; Fernando Franzini Java Blog</dc:creator>
		<pubDate>Thu, 16 Jul 2009 12:13:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-92098</guid>
		<description>[...] Retrieving entities [...]</description>
		<content:encoded><![CDATA[<p>[...] Retrieving entities [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tech Per</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-91491</link>
		<dc:creator>Tech Per</dc:creator>
		<pubDate>Mon, 06 Apr 2009 18:47:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-91491</guid>
		<description>Thanks for the viewpoints on methods for selecting for a use case. The FetchModel is interesting.</description>
		<content:encoded><![CDATA[<p>Thanks for the viewpoints on methods for selecting for a use case. The FetchModel is interesting.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-91490</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Mon, 06 Apr 2009 18:12:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-91490</guid>
		<description>&lt;strong&gt;@Tech, Andrew:&lt;/strong&gt; I think Andrew&#039;s suggestion seem pretty solid. I can see why having findOrdersWithOrderLinesLoadedSubmittedSince(…) methods would seem to violate the DAO pattern, but since I am not that much of a purist, I am not that much against them. ;-)

I think I actually prefer them over having an extra (defaulted) argument that specifies the fetch strategy. That smells too much like the Flag Argument I&#039;ve learned to abhor since reading &lt;a href=&quot;/2009/01/26/clean-code-vs-implementation-patterns/&quot; rel=&quot;nofollow&quot;&gt;Clean Code and Implementation Patterns&lt;/a&gt;.

In fact since reading those books I&#039;ve moved towards writing more shorter focused methods with descriptive (and therefore somtimes long) names instead of writing &quot;clever&quot; methods that can do all kinds of stuff when passed the right arguments. I guess that is why I&#039;m not too enthusiastic about Lars&#039; criteria/specification suggestion.
</description>
		<content:encoded><![CDATA[<p><strong>@Tech, Andrew:</strong> I think Andrew&#8217;s suggestion seem pretty solid. I can see why having findOrdersWithOrderLinesLoadedSubmittedSince(…) methods would seem to violate the DAO pattern, but since I am not that much of a purist, I am not that much against them. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>I think I actually prefer them over having an extra (defaulted) argument that specifies the fetch strategy. That smells too much like the Flag Argument I&#8217;ve learned to abhor since reading <a href="/2009/01/26/clean-code-vs-implementation-patterns/" rel="nofollow">Clean Code and Implementation Patterns</a>.</p>
<p>In fact since reading those books I&#8217;ve moved towards writing more shorter focused methods with descriptive (and therefore somtimes long) names instead of writing &#8220;clever&#8221; methods that can do all kinds of stuff when passed the right arguments. I guess that is why I&#8217;m not too enthusiastic about Lars&#8217; criteria/specification suggestion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-91489</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Mon, 06 Apr 2009 18:03:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-91489</guid>
		<description>&lt;strong&gt;@Lars:&lt;/strong&gt; I don&#039;t really have much to add on the Criteria thing after the comments by Dave and Andrew. :-) I guess it depends on what you use your DAO&#039;s for, how flexible you want them to be.

In the app we&#039;re building we know up front the kind of queries we want to do (no search forms), so having findByXXX methods make sense. They are lot more expressive than having the client build some kind of specification object. Be it JPA Criteria or your own class.

On the other hand if your app needs to support all kinds of ad hoc queries, such a mechanism makes sense.</description>
		<content:encoded><![CDATA[<p><strong>@Lars:</strong> I don&#8217;t really have much to add on the Criteria thing after the comments by Dave and Andrew. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  I guess it depends on what you use your DAO&#8217;s for, how flexible you want them to be.</p>
<p>In the app we&#8217;re building we know up front the kind of queries we want to do (no search forms), so having findByXXX methods make sense. They are lot more expressive than having the client build some kind of specification object. Be it JPA Criteria or your own class.</p>
<p>On the other hand if your app needs to support all kinds of ad hoc queries, such a mechanism makes sense.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Phillips</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-91484</link>
		<dc:creator>Andrew Phillips</dc:creator>
		<pubDate>Mon, 06 Apr 2009 11:23:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-91484</guid>
		<description>@Tech: &lt;em&gt;And I don’t want methods like findOrdersWithOrderLinesLoadedSubmittedSince(…) or the like. Any thoughts?&lt;/em&gt;

When we were last faced with this problem, we also decided against &lt;em&gt;findOrdersWithThisAndNotThat&lt;/em&gt;-type methods. In fact, we regarded them as violations of the DAO pattern (@Vincent: I&#039;m curious as to what your thoughts on this are). 

In our opinion, a DAO should be tailored to retrieve the information that the application needs, with different methods for different needs. But from a domain model perspective, &lt;em&gt;findOrdersWithThisAndThat&lt;/em&gt; and &lt;em&gt;findOrdersWithThisButNotThat&lt;/em&gt; return &lt;u&gt;the same information&lt;/u&gt;: a collection of &lt;tt&gt;Orders&lt;/tt&gt; meeting some criteria.

The &lt;em&gt;WithThis...&lt;/em&gt; bit thus does not identify a different business need, it is simply a &quot;hint&quot; that only a certain part of the returned object graph is required, and that the rest may be ignored. The usual assumption being that this will lead to a performance improvement.

But assume that, for some specific implementation, &lt;em&gt;findWithThisButNotThat&lt;/em&gt; is actually &lt;strong&gt;slower/more memory intensive/more &lt;em&gt;insert favourite performance metric here&lt;/em&gt;&lt;/strong&gt; than &lt;em&gt;findWithThisAndThat&lt;/em&gt;. Would it ever be called?

Our approach was to (attempt to) model what we felt the &lt;em&gt;WithThisAndThat&lt;/em&gt; actually represents: a &lt;strong&gt;suggestion&lt;/strong&gt; to the implementation that only certain properties are required, and others &lt;strong&gt;may&lt;/strong&gt; be ignored if this is likely to improve performance. We called it a &lt;tt&gt;FetchModel&lt;/tt&gt; and allowed the user to specify &lt;tt&gt;FetchTypes&lt;/tt&gt; (very similar to Hibernate&#039;s &lt;a href=&quot;http://www.hibernate.org/hib_docs/v3/api/org/hibernate/FetchMode.html&quot; target=&quot;new&quot; rel=&quot;nofollow&quot;&gt;FetchModels&lt;/a&gt;) for properties.

For those DAO methods for which this was relevant, we then had an overloaded method (let&#039;s defer the discussion about the evils of overloading for a minute)

&lt;pre lang=&quot;java&quot;&gt;
  /** Uses the default fetch model */
  Collection&lt;Order&gt; findOrdersSubmittedSince(Date date);

  Collection&lt;Order&gt; findOrdersSubmittedSince(Date date, FetchModel fetchModel);
&lt;/pre&gt;

where, in the case of your example, you would specify something like

&lt;pre lang=&quot;java&quot;&gt;
  FetchModel noOrderLinesFetchModel = new FetchModel(Order.class);
  noOrderLinesFetchModel.setFetchType(&quot;orderLines&quot;, FetchType.NONE);
&lt;/pre&gt;

It&#039;s then down to the implementation to interpret the fetch model, if desired.

Writing the code to actually implement a fetch model specification for a specific ORM framework is non-trivial, but if done with a bit of thought it can easily be reused.</description>
		<content:encoded><![CDATA[<p>@Tech: <em>And I don’t want methods like findOrdersWithOrderLinesLoadedSubmittedSince(…) or the like. Any thoughts?</em></p>
<p>When we were last faced with this problem, we also decided against <em>findOrdersWithThisAndNotThat</em>-type methods. In fact, we regarded them as violations of the DAO pattern (@Vincent: I&#8217;m curious as to what your thoughts on this are). </p>
<p>In our opinion, a DAO should be tailored to retrieve the information that the application needs, with different methods for different needs. But from a domain model perspective, <em>findOrdersWithThisAndThat</em> and <em>findOrdersWithThisButNotThat</em> return <u>the same information</u>: a collection of <tt>Orders</tt> meeting some criteria.</p>
<p>The <em>WithThis&#8230;</em> bit thus does not identify a different business need, it is simply a &#8220;hint&#8221; that only a certain part of the returned object graph is required, and that the rest may be ignored. The usual assumption being that this will lead to a performance improvement.</p>
<p>But assume that, for some specific implementation, <em>findWithThisButNotThat</em> is actually <strong>slower/more memory intensive/more <em>insert favourite performance metric here</em></strong> than <em>findWithThisAndThat</em>. Would it ever be called?</p>
<p>Our approach was to (attempt to) model what we felt the <em>WithThisAndThat</em> actually represents: a <strong>suggestion</strong> to the implementation that only certain properties are required, and others <strong>may</strong> be ignored if this is likely to improve performance. We called it a <tt>FetchModel</tt> and allowed the user to specify <tt>FetchTypes</tt> (very similar to Hibernate&#8217;s <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/FetchMode.html" target="new" rel="nofollow">FetchModels</a>) for properties.</p>
<p>For those DAO methods for which this was relevant, we then had an overloaded method (let&#8217;s defer the discussion about the evils of overloading for a minute)</p>
<pre lang="java">
  /** Uses the default fetch model */
  Collection<order> findOrdersSubmittedSince(Date date);

  Collection</order><order> findOrdersSubmittedSince(Date date, FetchModel fetchModel);
</order></pre>
<p>where, in the case of your example, you would specify something like</p>
<pre lang="java">
  FetchModel noOrderLinesFetchModel = new FetchModel(Order.class);
  noOrderLinesFetchModel.setFetchType("orderLines", FetchType.NONE);
</pre>
<p>It&#8217;s then down to the implementation to interpret the fetch model, if desired.</p>
<p>Writing the code to actually implement a fetch model specification for a specific ORM framework is non-trivial, but if done with a bit of thought it can easily be reused.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Phillips</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-91481</link>
		<dc:creator>Andrew Phillips</dc:creator>
		<pubDate>Mon, 06 Apr 2009 11:18:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-91481</guid>
		<description>@Lars, Dave: &lt;em&gt;In case of JPA 2.0 or SessionFactory you could use Criteria for this, but that would leak your implementation details to the client code.&lt;/em&gt;

I think that depends a little bit on whether you regard the use of a particular Criteria API as an &quot;implementation detail&quot;. 

If, for whatever reason, you &lt;em&gt;do&lt;/em&gt; decide to add a &quot;generic&quot; &lt;tt&gt;find(Criteria)&lt;/tt&gt; method to your DAO, you&#039;ll need to use &lt;em&gt;some&lt;/em&gt; kind of Criteria objects. Should you &lt;strong&gt;have&lt;/strong&gt; to reinvent this particular wheel if a suitable implementation already exists?

To be clear: this is only a question of perspective. If you decide to use the JPA Criteria in your DAO interface definition, you will still force clients to have a dependency on (that part of) JPA, which is something you may want/have to avoid.

But I think it&#039;s perfectly valid to interpret your interface&#039;s definition of &lt;tt&gt;find&lt;/tt&gt; as &quot;the client can specify a set of filter criteria, and it is the job of the DAO implementation to interpret these and return the correct results&quot;. And then, at least according to me, it&#039;s not implementation leakage.

In practice, of course, it may be difficult to use a certain Criteria API in any implementation other than the &quot;native&quot; one. 
Indeed, in previous projects I &lt;em&gt;have&lt;/em&gt; ended up reinventing some of this wheel and writing my own Criteria API, together with a converter that converted &quot;my&quot; criteria to Hibernate criteria, for use in a Hibernate implementation.
In this case, the reasons were, in fact, less related to enforced Hibernate dependencies. Instead, the key considerations were to 

&lt;ul&gt;
&lt;li&gt;use only criteria objects that could be easily and cheaply serialized and constructed in other programming languages and&lt;/li&gt;
&lt;li&gt;limit the expressiveness of the critiera API &lt;em&gt;up front&lt;/em&gt; (i.e. on the client side), rather than returning &lt;tt&gt;CriteriaNotSupported&lt;/tt&gt;-type errors later. We didn&#039;t want to allow &quot;A or B&quot;, for instance, only &quot;A and B&quot;.&lt;/li&gt;
&lt;/ul&gt;</description>
		<content:encoded><![CDATA[<p>@Lars, Dave: <em>In case of JPA 2.0 or SessionFactory you could use Criteria for this, but that would leak your implementation details to the client code.</em></p>
<p>I think that depends a little bit on whether you regard the use of a particular Criteria API as an &#8220;implementation detail&#8221;. </p>
<p>If, for whatever reason, you <em>do</em> decide to add a &#8220;generic&#8221; <tt>find(Criteria)</tt> method to your DAO, you&#8217;ll need to use <em>some</em> kind of Criteria objects. Should you <strong>have</strong> to reinvent this particular wheel if a suitable implementation already exists?</p>
<p>To be clear: this is only a question of perspective. If you decide to use the JPA Criteria in your DAO interface definition, you will still force clients to have a dependency on (that part of) JPA, which is something you may want/have to avoid.</p>
<p>But I think it&#8217;s perfectly valid to interpret your interface&#8217;s definition of <tt>find</tt> as &#8220;the client can specify a set of filter criteria, and it is the job of the DAO implementation to interpret these and return the correct results&#8221;. And then, at least according to me, it&#8217;s not implementation leakage.</p>
<p>In practice, of course, it may be difficult to use a certain Criteria API in any implementation other than the &#8220;native&#8221; one.<br />
Indeed, in previous projects I <em>have</em> ended up reinventing some of this wheel and writing my own Criteria API, together with a converter that converted &#8220;my&#8221; criteria to Hibernate criteria, for use in a Hibernate implementation.<br />
In this case, the reasons were, in fact, less related to enforced Hibernate dependencies. Instead, the key considerations were to </p>
<ul>
<li>use only criteria objects that could be easily and cheaply serialized and constructed in other programming languages and</li>
<li>limit the expressiveness of the critiera API <em>up front</em> (i.e. on the client side), rather than returning <tt>CriteriaNotSupported</tt>-type errors later. We didn&#8217;t want to allow &#8220;A or B&#8221;, for instance, only &#8220;A and B&#8221;.</li>
</ul>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-91478</link>
		<dc:creator>Lars</dc:creator>
		<pubDate>Sun, 05 Apr 2009 19:13:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-91478</guid>
		<description>@Dave

No need to apologise, I am glad you answered your own question :-).</description>
		<content:encoded><![CDATA[<p>@Dave</p>
<p>No need to apologise, I am glad you answered your own question <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://blog.xebia.com/2009/04/03/jpa-implementation-patterns-retrieving-entities/comment-page-1/#comment-91477</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Sun, 05 Apr 2009 16:07:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=916#comment-91477</guid>
		<description>@Lars

LOL...serves me right for posting *before* actually reading the chapter on Specifications in Evan&#039;s DDD book. Turns out that implementing a Specification to return a Criteria object (or SQL string, if you like) to be executed by your domain Repository is perfectly valid, and gets around having to ship your objects across the wire to filter them. So Specs really do mesh well with Repositories (which is explicitly stated in the book). Apologies! (and apologies for calling you Vincent by mistake in the first post LOL).

Dave</description>
		<content:encoded><![CDATA[<p>@Lars</p>
<p>LOL&#8230;serves me right for posting *before* actually reading the chapter on Specifications in Evan&#8217;s DDD book. Turns out that implementing a Specification to return a Criteria object (or SQL string, if you like) to be executed by your domain Repository is perfectly valid, and gets around having to ship your objects across the wire to filter them. So Specs really do mesh well with Repositories (which is explicitly stated in the book). Apologies! (and apologies for calling you Vincent by mistake in the first post LOL).</p>
<p>Dave</p>
]]></content:encoded>
	</item>
</channel>
</rss>
