<?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: Lazy loading</title>
	<atom:link href="http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/</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: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92559</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Sun, 27 Sep 2009 17:33:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92559</guid>
		<description>&lt;strong&gt;@Simon Massey:&lt;/strong&gt; Your objections to the Open Session in View patterns are interesting. The fact that the database connection is still held during the rendering phase could degrade performance, but this will also depend on how long that phase takes compared to the actual business logic invoked. I would be interested to see compare some numbers there.

Especially when you take out the time spent on loading lazy objects during the rendering phase. Because that time would move to the DTO-creation phase when using DTO&#039;s or the make-sure-every-entity-is-initialized-phase in your approach, making the pre-rendering phase longer.

Your last paragraph is a good summary: use the Open Session in View when you can, and only introduce Service Facades and/or DTO&#039;s when you need to. This is similar to what I just replied to your question on &lt;a href=&quot;/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/&quot; rel=&quot;nofollow&quot;&gt;the blog about Service Facades and DTO&#039;s&lt;/a&gt;. I guess you can say &quot;it depends&quot;. ;-)</description>
		<content:encoded><![CDATA[<p><strong>@Simon Massey:</strong> Your objections to the Open Session in View patterns are interesting. The fact that the database connection is still held during the rendering phase could degrade performance, but this will also depend on how long that phase takes compared to the actual business logic invoked. I would be interested to see compare some numbers there.</p>
<p>Especially when you take out the time spent on loading lazy objects during the rendering phase. Because that time would move to the DTO-creation phase when using DTO&#8217;s or the make-sure-every-entity-is-initialized-phase in your approach, making the pre-rendering phase longer.</p>
<p>Your last paragraph is a good summary: use the Open Session in View when you can, and only introduce Service Facades and/or DTO&#8217;s when you need to. This is similar to what I just replied to your question on <a href="/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/" rel="nofollow">the blog about Service Facades and DTO&#8217;s</a>. I guess you can say &#8220;it depends&#8221;. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Massey</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92535</link>
		<dc:creator>Simon Massey</dc:creator>
		<pubDate>Fri, 25 Sep 2009 11:28:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92535</guid>
		<description>@Vincent: &quot;These are the two patterns I’ve seen most often in real applications. What don’t you like about them?&quot;

The DTO model is discussed extensively on the next blog post but I thought that I would say when I would choose not to use Open Session In View. Open Session In View (on a spring based webapp) tethers the web container thread to the database connection. If I where to assign twice as many web threads as the maximum size of my db connection pool it would have little or no effect. Web threads would simply queue for a db connection to be returned to the pool. More significantly the db transaction (when using JTA) would be active over the rendering phase and that can cause increased contention and increased resource pinning within the database. (Without JTA things like JPA transactions actually do a single phase jdbc transaction for the duration of the flush which occurs when the session is closed. This makes holding the transaction open for longer less of an issue when you don&#039;t require JTA).  

If I go with a facade instead and scope my db transaction to a façade the db connection will be returned to the pool when the façades returns. I can then move my UI rendering to after the façade method returns and also perform some none-db based UI input validation before calling the façade. I can then keep more web threads working than I have database connections and have shorter JTA transactions.  So I can horizontally scale my application layer infront of my db to better effect. 

If I know that my app will never need to be horizontally scaled as it is low volume app and I can live with tethering the db pool to my rendering phase then Open Session In View is much easier to develop with so I would use that. (We use the ZK RIA framwork which even has explicit support for Hibernate Session In View). In contrast if I know that I will need to scale out then the added effort of working via façades is the cost of being able to scale out. If the app is low complexity such that the façades between UI controllers and the services looks like too much layering then I might consider in-lining the façade logic into one of the other layers within a spring transaction template which scopes the db transaction (and hence the db transaction).</description>
		<content:encoded><![CDATA[<p>@Vincent: &#8220;These are the two patterns I’ve seen most often in real applications. What don’t you like about them?&#8221;</p>
<p>The DTO model is discussed extensively on the next blog post but I thought that I would say when I would choose not to use Open Session In View. Open Session In View (on a spring based webapp) tethers the web container thread to the database connection. If I where to assign twice as many web threads as the maximum size of my db connection pool it would have little or no effect. Web threads would simply queue for a db connection to be returned to the pool. More significantly the db transaction (when using JTA) would be active over the rendering phase and that can cause increased contention and increased resource pinning within the database. (Without JTA things like JPA transactions actually do a single phase jdbc transaction for the duration of the flush which occurs when the session is closed. This makes holding the transaction open for longer less of an issue when you don&#8217;t require JTA).  </p>
<p>If I go with a facade instead and scope my db transaction to a façade the db connection will be returned to the pool when the façades returns. I can then move my UI rendering to after the façade method returns and also perform some none-db based UI input validation before calling the façade. I can then keep more web threads working than I have database connections and have shorter JTA transactions.  So I can horizontally scale my application layer infront of my db to better effect. </p>
<p>If I know that my app will never need to be horizontally scaled as it is low volume app and I can live with tethering the db pool to my rendering phase then Open Session In View is much easier to develop with so I would use that. (We use the ZK RIA framwork which even has explicit support for Hibernate Session In View). In contrast if I know that I will need to scale out then the added effort of working via façades is the cost of being able to scale out. If the app is low complexity such that the façades between UI controllers and the services looks like too much layering then I might consider in-lining the façade logic into one of the other layers within a spring transaction template which scopes the db transaction (and hence the db transaction).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Massey</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92517</link>
		<dc:creator>Simon Massey</dc:creator>
		<pubDate>Wed, 23 Sep 2009 10:22:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92517</guid>
		<description>@Vincent I posted a comment over on the other blog where Erik was posting. I will move my discussion over there to your blog on Service Facade and DTOs.</description>
		<content:encoded><![CDATA[<p>@Vincent I posted a comment over on the other blog where Erik was posting. I will move my discussion over there to your blog on Service Facade and DTOs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92477</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Fri, 18 Sep 2009 09:52:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92477</guid>
		<description>&lt;strong&gt;@Simon Massey:&lt;/strong&gt; By the &quot;option 2 pattern&quot;, do you mean using either a Service Facade with DTO&#039;s or using the open EntityManager in view pattern? These are the two patterns I&#039;ve seen most often in real applications. What don&#039;t you like about them?

If I understand correctly, your suggestion is a mix between the two approaches. There is a Service Facade but instead of using DTO&#039;s you have the Service Facade make sure all domain objects are correctly initialized so that you can just return them as-is. 

That would work, but it makes the clients layer depend on the domain objects. Which brings me nicely to your other point. ;-)

Whether that is good thing or a bad thing is an interesting discussion. Your point that it can make inexperienced developers just write a lot or unnecessary, incorrect, inefficient boilerplate code is a good one. Then again, inexperienced developers are likely to make other mistakes. So then the questions is: do we want architecture to &quot;defend&quot; against  inexperienced developers. Or do we perform code reviews andteach how to use the architecture correctly? But that is a different issue altogether. 

Back to the point: a good reason to have that isolation layer of the Service Facade/DTO combo is so that you can modify your domain objects without it having a direct impact on your client(s). But that depends on how closely the two are tied. Erik Rozendaal&#039;s comment to &lt;a href=&quot;http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/&quot; rel=&quot;nofollow&quot;&gt;my blog on Service Facade and DTO&#039;s&lt;/a&gt; has some good points on this.</description>
		<content:encoded><![CDATA[<p><strong>@Simon Massey:</strong> By the &#8220;option 2 pattern&#8221;, do you mean using either a Service Facade with DTO&#8217;s or using the open EntityManager in view pattern? These are the two patterns I&#8217;ve seen most often in real applications. What don&#8217;t you like about them?</p>
<p>If I understand correctly, your suggestion is a mix between the two approaches. There is a Service Facade but instead of using DTO&#8217;s you have the Service Facade make sure all domain objects are correctly initialized so that you can just return them as-is. </p>
<p>That would work, but it makes the clients layer depend on the domain objects. Which brings me nicely to your other point. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Whether that is good thing or a bad thing is an interesting discussion. Your point that it can make inexperienced developers just write a lot or unnecessary, incorrect, inefficient boilerplate code is a good one. Then again, inexperienced developers are likely to make other mistakes. So then the questions is: do we want architecture to &#8220;defend&#8221; against  inexperienced developers. Or do we perform code reviews andteach how to use the architecture correctly? But that is a different issue altogether. </p>
<p>Back to the point: a good reason to have that isolation layer of the Service Facade/DTO combo is so that you can modify your domain objects without it having a direct impact on your client(s). But that depends on how closely the two are tied. Erik Rozendaal&#8217;s comment to <a href="http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/" rel="nofollow">my blog on Service Facade and DTO&#8217;s</a> has some good points on this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Massey</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92475</link>
		<dc:creator>Simon Massey</dc:creator>
		<pubDate>Thu, 17 Sep 2009 22:50:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92475</guid>
		<description>I also love this series. It has even almost turned me to the dark-side of accepting that DAO is the correct approach...

I agree with @Dimitris that the &quot;option 2&quot; approaches seem unsatisfactory. Why not just have façades that are use case orientated and @transactional with methods which know how to initialize lazy objects (or call the correct eager queries) and have clients such as the web UI always go via the façades? 

I found the following statement strange: 

    &quot;While purists may argue that this makes your presentation layer depends on your domain objects, it is a compelling approach for simple web applications.&quot;

Surely I want all of my platform at all layers dependent upon my domain objects as they model the business problem space? Anything built to be a business solution should be familiar with, and make reference to, the problem domain. I tend to draw the &quot;domain layer&quot; as a vertical stack to the side of my &quot;cake diagram&quot; of the application logical layers and tell all of the developers that they must try to stick almost exclusively to using the domain objects wherever possible. 

We use a highly stateful ajax framework (ZK) for very complex web applications that has a declarative bi-directional databindings feature. I can then bind my domain objects to my web desktop components. So I use my domain objects everywhere within my presentation layer as detached objects. We don&#039;t use open session in view to avoid the scalability issue of leaving the session open whilst building the view. The use case orientated façades that scope the transaction can take care of calling the re-attach logic before any save which you covered in your previous blog on saving detached entities. 

I got a cold shiver when I read the recommended strategy of copying the domain objects to DTOs or VOs. On large projects the programmers that don&#039;t have a strong experience of JPA/JDO/Hibernate they are going to think &quot;DTO = result set&quot; and fall back to obsolete patterns. Pretty soon you get an explosion of different but mostly similar DTOs that model the screens that the developers are building. They then makes lots of trips down the stack to pull other similar looking DTO by ID when the current one is not quite the right. JDBC IO saturation ensues and the domain pattern and ORM can go right out of the window.</description>
		<content:encoded><![CDATA[<p>I also love this series. It has even almost turned me to the dark-side of accepting that DAO is the correct approach&#8230;</p>
<p>I agree with @Dimitris that the &#8220;option 2&#8243; approaches seem unsatisfactory. Why not just have façades that are use case orientated and @transactional with methods which know how to initialize lazy objects (or call the correct eager queries) and have clients such as the web UI always go via the façades? </p>
<p>I found the following statement strange: </p>
<p>    &#8220;While purists may argue that this makes your presentation layer depends on your domain objects, it is a compelling approach for simple web applications.&#8221;</p>
<p>Surely I want all of my platform at all layers dependent upon my domain objects as they model the business problem space? Anything built to be a business solution should be familiar with, and make reference to, the problem domain. I tend to draw the &#8220;domain layer&#8221; as a vertical stack to the side of my &#8220;cake diagram&#8221; of the application logical layers and tell all of the developers that they must try to stick almost exclusively to using the domain objects wherever possible. </p>
<p>We use a highly stateful ajax framework (ZK) for very complex web applications that has a declarative bi-directional databindings feature. I can then bind my domain objects to my web desktop components. So I use my domain objects everywhere within my presentation layer as detached objects. We don&#8217;t use open session in view to avoid the scalability issue of leaving the session open whilst building the view. The use case orientated façades that scope the transaction can take care of calling the re-attach logic before any save which you covered in your previous blog on saving detached entities. </p>
<p>I got a cold shiver when I read the recommended strategy of copying the domain objects to DTOs or VOs. On large projects the programmers that don&#8217;t have a strong experience of JPA/JDO/Hibernate they are going to think &#8220;DTO = result set&#8221; and fall back to obsolete patterns. Pretty soon you get an explosion of different but mostly similar DTOs that model the screens that the developers are building. They then makes lots of trips down the stack to pull other similar looking DTO by ID when the current one is not quite the right. JDBC IO saturation ensues and the domain pattern and ORM can go right out of the window.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: allan</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92278</link>
		<dc:creator>allan</dc:creator>
		<pubDate>Tue, 11 Aug 2009 07:23:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92278</guid>
		<description>For step 2, another option might be, make the DAO statefull session bean and use extended PersistenceContext.</description>
		<content:encoded><![CDATA[<p>For step 2, another option might be, make the DAO statefull session bean and use extended PersistenceContext.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92161</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Sat, 25 Jul 2009 09:22:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92161</guid>
		<description>&lt;strong&gt;@Alvin:&lt;/strong&gt; Are you saying that if my Order contains not only a set of OrderLines but also a set of Customers both of these collections would be loaded whenever I access either one of them? That is not something I have experienced with Hibernate. But I guess this would be JPA provider dependent as the JPA spec only says the LAZY annotation is a &quot;hint&quot; to the JPA provider. In which JPA provider have you seen this behaviour?

Something I &lt;strong&gt;have&lt;/strong&gt; noticed is that accessing a collection causes Hibernate to load &lt;em&gt;all&lt;/em&gt; the entities in that collection, even if you are just adding or removing an entity. This can become a problem when you have large collections and especially when &lt;a href=&quot;/2009/05/25/jpa-implementation-patterns-bidirectional-associations-vs-lazy-loading/&quot; rel=&quot;nofollow&quot;&gt;combined with managing bidirectional associations&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p><strong>@Alvin:</strong> Are you saying that if my Order contains not only a set of OrderLines but also a set of Customers both of these collections would be loaded whenever I access either one of them? That is not something I have experienced with Hibernate. But I guess this would be JPA provider dependent as the JPA spec only says the LAZY annotation is a &#8220;hint&#8221; to the JPA provider. In which JPA provider have you seen this behaviour?</p>
<p>Something I <strong>have</strong> noticed is that accessing a collection causes Hibernate to load <em>all</em> the entities in that collection, even if you are just adding or removing an entity. This can become a problem when you have large collections and especially when <a href="/2009/05/25/jpa-implementation-patterns-bidirectional-associations-vs-lazy-loading/" rel="nofollow">combined with managing bidirectional associations</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alvin</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92105</link>
		<dc:creator>alvin</dc:creator>
		<pubDate>Thu, 16 Jul 2009 14:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92105</guid>
		<description>Something which I see people forgetting to mention is that if you have multiple lazily loaded collections on a single entity - the first time you access one of those collections (causing it to load) - it will also load the other.</description>
		<content:encoded><![CDATA[<p>Something which I see people forgetting to mention is that if you have multiple lazily loaded collections on a single entity &#8211; the first time you access one of those collections (causing it to load) &#8211; it will also load the other.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JPA Implementation Patterns &#171; Fernando Franzini Java Blog</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92102</link>
		<dc:creator>JPA Implementation Patterns &#171; Fernando Franzini Java Blog</dc:creator>
		<pubDate>Thu, 16 Jul 2009 12:43:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92102</guid>
		<description>[...] Lazy loading [...]</description>
		<content:encoded><![CDATA[<p>[...] Lazy loading [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eswar</title>
		<link>http://blog.xebia.com/2009/04/27/jpa-implementation-patterns-lazy-loading/comment-page-1/#comment-92057</link>
		<dc:creator>Eswar</dc:creator>
		<pubDate>Sat, 11 Jul 2009 05:45:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1540#comment-92057</guid>
		<description>Excellent information.</description>
		<content:encoded><![CDATA[<p>Excellent information.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
