<?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: Data Access Objects</title>
	<atom:link href="http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/</link>
	<description></description>
	<lastBuildDate>Thu, 11 Mar 2010 16:23:01 +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/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-94021</link>
		<dc:creator>JPA Implementation Patterns &#124; Upthrust</dc:creator>
		<pubDate>Sat, 30 Jan 2010 11:34:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-94021</guid>
		<description>[...] Data Access Objects [...]</description>
		<content:encoded><![CDATA[<p>[...] Data Access Objects [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tbee</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-94005</link>
		<dc:creator>tbee</dc:creator>
		<pubDate>Tue, 26 Jan 2010 14:11:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-94005</guid>
		<description>It is interesting to see how everyone seems to only have the word &quot;web&quot; between their ears when talking about persistence / JPA. I currently have a business model that is used both in a web (SOAP) context and a swing application. So I have no use for @PersistenceContext, because my EMs in the swing application are bound to the JFrames, but the same BM must also run in a webalike context, where the EM&#039;s are bound to requests. 

I decided to go for a &quot;EntityManagerFinder&quot;, which can be configured to bind either way. And because I do not use @PersistenceContext there is no need for me to have DAO instances where this annotation can be set. The method DAO can become statics and then you can get rid of the DAO interface as well. And since they&#039;re now just static methods, it&#039;s much more convenient to move them into the entity class.

Order.findById(...)
Order.findAll()
etc</description>
		<content:encoded><![CDATA[<p>It is interesting to see how everyone seems to only have the word &#8220;web&#8221; between their ears when talking about persistence / JPA. I currently have a business model that is used both in a web (SOAP) context and a swing application. So I have no use for @PersistenceContext, because my EMs in the swing application are bound to the JFrames, but the same BM must also run in a webalike context, where the EM&#8217;s are bound to requests. </p>
<p>I decided to go for a &#8220;EntityManagerFinder&#8221;, which can be configured to bind either way. And because I do not use @PersistenceContext there is no need for me to have DAO instances where this annotation can be set. The method DAO can become statics and then you can get rid of the DAO interface as well. And since they&#8217;re now just static methods, it&#8217;s much more convenient to move them into the entity class.</p>
<p>Order.findById(&#8230;)<br />
Order.findAll()<br />
etc</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Massey</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92531</link>
		<dc:creator>Simon Massey</dc:creator>
		<pubDate>Thu, 24 Sep 2009 14:47:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92531</guid>
		<description>I am with @Rodrigo that the word DAO is not an orm term it is too heavily associated with the failures of EJB2 where folk just wrote their own JDBC result set mapper loading DTOs. In that universe as mentioned by @Aurelio you hide the DAO from the clients of your services - it is at the bottom of the stack. That pattern has been around for a decade and a huge set of systems still work that way so it has &quot;trademark ownership&quot; of that word. (Now I am starting to feel real old taking about the &quot;bad old days&quot;.)

If we look at Chris Richardson&#039;s (pre-JPA) book POJOs In Action he too wants to have an abstraction for grouping queries or optional eager loads. He calls them &quot;Repositories&quot;. He actually has sample code which demonstrates swapping between JDO and Hibernate implementations. Clearly with JPA we don&#039;t really need the swap out capabilities. The book explains who the iBatis framework can be used with an alternative set of true DAO/DTO patters to do some high performance search screens without having to pollute your use of ORM by forcing it to &quot;play DTO mapper&quot;. 

Where the difference really stands out between what is an old school DAO and how we might want to do things with JPA is the following: with the Chris Richardson approach he names the Repository classes to be part of his domain model that you expose to the client / façades. That is certainly not what you do with an old school DAO from the EJB2 world which was usually deemed to be strictly in the data layer and never to be touched except via a service (as @Carlos says). JPA however makes it respectable to get closer to your persistence store than it was to get close to JDBC, Oracle jPub (shudder), or some-home-brew-dto-mapper. That is one of the point that @Aurelio makes. 

Saying that the Repository (aka your JPA DOA) is part of the Domain Model makes sense when we consider that the abstraction is mainly there to encapsulate how findByThisAndThat(...) methods actually set the named parameters of a named query. Any façade which wants to intact with record data structure exposed entities would want to call such finders to obtain things which it would then work with (via services) then perhaps call further loadThat/findOther methods on the Repository (aka your DOA) required to update the presentation view. 

I wrote a demo app (for an AJAX framework) and copied the GenericJpaDao that I found on the Loom project. They kindly pointed out that they had upgraded their framework to use an ExtendedEntityManager: 

http://loom.svn.sourceforge.net/viewvc/loom/loom/trunk/src/java/org/loom/persistence/ExtendedEntityManagerOperations.java?revision=1682&amp;view=markup

Although I have not tried that I think that looks like it is in alignment with what the Chris Richardson book was documenting pre-JPA in terms of being the respectable &quot;Domain Store&quot; which @Aurelio makes reference to which can be exposed to your façades as part of your Domain Model. Of course it is just a re-labelling of the generic DAO but as the Clean Code book says labels are important (thanks for your post about that book I bought it based on your mention of it).</description>
		<content:encoded><![CDATA[<p>I am with @Rodrigo that the word DAO is not an orm term it is too heavily associated with the failures of EJB2 where folk just wrote their own JDBC result set mapper loading DTOs. In that universe as mentioned by @Aurelio you hide the DAO from the clients of your services &#8211; it is at the bottom of the stack. That pattern has been around for a decade and a huge set of systems still work that way so it has &#8220;trademark ownership&#8221; of that word. (Now I am starting to feel real old taking about the &#8220;bad old days&#8221;.)</p>
<p>If we look at Chris Richardson&#8217;s (pre-JPA) book POJOs In Action he too wants to have an abstraction for grouping queries or optional eager loads. He calls them &#8220;Repositories&#8221;. He actually has sample code which demonstrates swapping between JDO and Hibernate implementations. Clearly with JPA we don&#8217;t really need the swap out capabilities. The book explains who the iBatis framework can be used with an alternative set of true DAO/DTO patters to do some high performance search screens without having to pollute your use of ORM by forcing it to &#8220;play DTO mapper&#8221;. </p>
<p>Where the difference really stands out between what is an old school DAO and how we might want to do things with JPA is the following: with the Chris Richardson approach he names the Repository classes to be part of his domain model that you expose to the client / façades. That is certainly not what you do with an old school DAO from the EJB2 world which was usually deemed to be strictly in the data layer and never to be touched except via a service (as @Carlos says). JPA however makes it respectable to get closer to your persistence store than it was to get close to JDBC, Oracle jPub (shudder), or some-home-brew-dto-mapper. That is one of the point that @Aurelio makes. </p>
<p>Saying that the Repository (aka your JPA DOA) is part of the Domain Model makes sense when we consider that the abstraction is mainly there to encapsulate how findByThisAndThat(&#8230;) methods actually set the named parameters of a named query. Any façade which wants to intact with record data structure exposed entities would want to call such finders to obtain things which it would then work with (via services) then perhaps call further loadThat/findOther methods on the Repository (aka your DOA) required to update the presentation view. </p>
<p>I wrote a demo app (for an AJAX framework) and copied the GenericJpaDao that I found on the Loom project. They kindly pointed out that they had upgraded their framework to use an ExtendedEntityManager: </p>
<p><a href="http://loom.svn.sourceforge.net/viewvc/loom/loom/trunk/src/java/org/loom/persistence/ExtendedEntityManagerOperations.java?revision=1682&amp;view=markup" rel="nofollow">http://loom.svn.sourceforge.net/viewvc/loom/loom/trunk/src/java/org/loom/persistence/ExtendedEntityManagerOperations.java?revision=1682&amp;view=markup</a></p>
<p>Although I have not tried that I think that looks like it is in alignment with what the Chris Richardson book was documenting pre-JPA in terms of being the respectable &#8220;Domain Store&#8221; which @Aurelio makes reference to which can be exposed to your façades as part of your Domain Model. Of course it is just a re-labelling of the generic DAO but as the Clean Code book says labels are important (thanks for your post about that book I bought it based on your mention of it).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92341</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Mon, 24 Aug 2009 18:17:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92341</guid>
		<description>&lt;strong&gt;@alex:&lt;/strong&gt; The JPA specification, the one supported by Hibernate, is a subset of the EJB 3.0 specification that only deals with persistence.  The &lt;a href=&quot;http://java.sun.com/javaee/5/docs/api/javax/ejb/EJB.html&quot; rel=&quot;nofollow&quot;&gt;&lt;tt&gt;@EJB&lt;/tt&gt;&lt;/a&gt; annotation is part of the EJB 3.0 specification and not of the JPA subset and as such is not supported by Hibernate.</description>
		<content:encoded><![CDATA[<p><strong>@alex:</strong> The JPA specification, the one supported by Hibernate, is a subset of the EJB 3.0 specification that only deals with persistence.  The <a href="http://java.sun.com/javaee/5/docs/api/javax/ejb/EJB.html" rel="nofollow"><tt>@EJB</tt></a> annotation is part of the EJB 3.0 specification and not of the JPA subset and as such is not supported by Hibernate.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alex</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92312</link>
		<dc:creator>alex</dc:creator>
		<pubDate>Wed, 19 Aug 2009 08:42:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92312</guid>
		<description>I&#039;m trying jpa with hibernate as provider and @EJB(name=&quot;xxx&quot;) annotation cannot be resolved.
How can I solve this problem ?
Thank you.</description>
		<content:encoded><![CDATA[<p>I&#8217;m trying jpa with hibernate as provider and @EJB(name=&#8221;xxx&#8221;) annotation cannot be resolved.<br />
How can I solve this problem ?<br />
Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Twitted by bubbl</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92243</link>
		<dc:creator>Twitted by bubbl</dc:creator>
		<pubDate>Tue, 04 Aug 2009 09:19:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92243</guid>
		<description>[...] This post was Twitted by bubbl [...]</description>
		<content:encoded><![CDATA[<p>[...] This post was Twitted by bubbl [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JPA : A t&#8217;on vraiment besoin de DAO ? - blog de hugo geissman</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92220</link>
		<dc:creator>JPA : A t&#8217;on vraiment besoin de DAO ? - blog de hugo geissman</dc:creator>
		<pubDate>Sat, 01 Aug 2009 15:37:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92220</guid>
		<description>[...] Cet article est une proposition de traduction de l&#8217;article de Vicent Patington : Do we really need DAO ?  [...]</description>
		<content:encoded><![CDATA[<p>[...] Cet article est une proposition de traduction de l&#8217;article de Vicent Patington : Do we really need DAO ?  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92185</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Mon, 27 Jul 2009 16:26:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92185</guid>
		<description>&lt;strong&gt;@Nicolas Le Coz:&lt;/strong&gt; The JPA spec says that EntityManager.merge will return a reference to another object than the one passed in when the object was already loaded in the current context. So that explains why a would sometime change. But to really make it work you would have to capture the result of the merge method like so:

&lt;pre lang=&quot;java&quot;&gt;
public class monDaoImpl implements monDao {
    …
    public monEntity save(monEntity e) {
        if (e.getId() == null) {
            entityManager.persist(e);
        }
        else {
            e = entityManager.merge(e);
        }
    }
    return e ;
}
&lt;/pre&gt;

For more details on this please read &lt;a href=&quot;/2009/03/23/jpa-implementation-patterns-saving-detached-entities/&quot; rel=&quot;nofollow&quot;&gt;the blog on saving (detached) entities&lt;/a&gt;.

</description>
		<content:encoded><![CDATA[<p><strong>@Nicolas Le Coz:</strong> The JPA spec says that EntityManager.merge will return a reference to another object than the one passed in when the object was already loaded in the current context. So that explains why a would sometime change. But to really make it work you would have to capture the result of the merge method like so:</p>
<pre lang="java">
public class monDaoImpl implements monDao {
    …
    public monEntity save(monEntity e) {
        if (e.getId() == null) {
            entityManager.persist(e);
        }
        else {
            e = entityManager.merge(e);
        }
    }
    return e ;
}
</pre>
<p>For more details on this please read <a href="/2009/03/23/jpa-implementation-patterns-saving-detached-entities/" rel="nofollow">the blog on saving (detached) entities</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolas Le Coz</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92178</link>
		<dc:creator>Nicolas Le Coz</dc:creator>
		<pubDate>Mon, 27 Jul 2009 09:44:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92178</guid>
		<description>Hello Vincent,

Thanks for your articles.

I have one question, during my project we have realize DAOs like this :
&lt;pre lang=&quot;java&quot;&gt;
public class myDaoImpl implements myDao {
    …
    public void save(Entity e) {
        if (e.getId() == null) {
            entityManager.persist(e);
        }
        else {
            entityManager.merge(e);
        }
    }
    …
}
&lt;/pre&gt;

But after several mistakes on lazy loading we transform on (we add return e):

&lt;pre lang=&quot;java&quot;&gt;
public class monDaoImpl implements monDao {
    …
    public monEntity save(monEntity e) {
        if (e.getId() == null) {
            entityManager.persist(e);
        }
        else {
            entityManager.merge(e);
        }
    }
    return e ;
}
&lt;/pre&gt;

We add &quot;return e&quot;, because it seems that Hibernate changes the reference of entity, even if the JPA method signature is &quot;void persist(Object o)&quot; but the other sign &quot;Object merge(Object o)&quot;. After call of persist Hibernate changes the reference of entity to a proxy to manage entity that is monitored by Hibernate&quot;. So, outside the method &quot;save&quot; we lost reference of Hibernate proxy on entity, and we must to reattach then entity to Hibernate session.

you confirm it? Is it a specificity of Hibernate and not JPA?

Nicolas Le Coz (Xebia FR)</description>
		<content:encoded><![CDATA[<p>Hello Vincent,</p>
<p>Thanks for your articles.</p>
<p>I have one question, during my project we have realize DAOs like this :</p>
<pre lang="java">
public class myDaoImpl implements myDao {
    …
    public void save(Entity e) {
        if (e.getId() == null) {
            entityManager.persist(e);
        }
        else {
            entityManager.merge(e);
        }
    }
    …
}
</pre>
<p>But after several mistakes on lazy loading we transform on (we add return e):</p>
<pre lang="java">
public class monDaoImpl implements monDao {
    …
    public monEntity save(monEntity e) {
        if (e.getId() == null) {
            entityManager.persist(e);
        }
        else {
            entityManager.merge(e);
        }
    }
    return e ;
}
</pre>
<p>We add &#8220;return e&#8221;, because it seems that Hibernate changes the reference of entity, even if the JPA method signature is &#8220;void persist(Object o)&#8221; but the other sign &#8220;Object merge(Object o)&#8221;. After call of persist Hibernate changes the reference of entity to a proxy to manage entity that is monitored by Hibernate&#8221;. So, outside the method &#8220;save&#8221; we lost reference of Hibernate proxy on entity, and we must to reattach then entity to Hibernate session.</p>
<p>you confirm it? Is it a specificity of Hibernate and not JPA?</p>
<p>Nicolas Le Coz (Xebia FR)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/comment-page-1/#comment-92123</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Sun, 19 Jul 2009 14:13:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=900#comment-92123</guid>
		<description>&lt;strong&gt;@Carlos:&lt;/strong&gt; The reason I use this DAO pattern is to prevent business logic from having to directly use the JPA API, which is especially cumbersome when writing queries. Switching to a different persistence mechanism is not a scenario I envision, so the full capabilities of JPA are exposed, such as being able to navigate the object graph. If that means that this pattern should be called something other than DAO, well, please see my comment above. ;-)</description>
		<content:encoded><![CDATA[<p><strong>@Carlos:</strong> The reason I use this DAO pattern is to prevent business logic from having to directly use the JPA API, which is especially cumbersome when writing queries. Switching to a different persistence mechanism is not a scenario I envision, so the full capabilities of JPA are exposed, such as being able to navigate the object graph. If that means that this pattern should be called something other than DAO, well, please see my comment above. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
