<?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: Saving (detached) entities</title>
	<atom:link href="http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-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: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-94128</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Mon, 15 Feb 2010 20:14:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-94128</guid>
		<description>&lt;strong&gt;Fabrice Leray:&lt;/strong&gt; I am not familiar with Seam and the way it manages the persistence context. From what I understand your problem is that the context is scoped too large. I always use a request scoped session and that makes the undo pretty easy: just throw an exception and the transaction gets rolled back immediately.

Quite straightforward actually or maybe I&#039;m not understanding you correctly. Could you post an example?</description>
		<content:encoded><![CDATA[<p><strong>Fabrice Leray:</strong> I am not familiar with Seam and the way it manages the persistence context. From what I understand your problem is that the context is scoped too large. I always use a request scoped session and that makes the undo pretty easy: just throw an exception and the transaction gets rolled back immediately.</p>
<p>Quite straightforward actually or maybe I&#8217;m not understanding you correctly. Could you post an example?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabrice Leray</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-94081</link>
		<dc:creator>Fabrice Leray</dc:creator>
		<pubDate>Wed, 10 Feb 2010 23:38:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-94081</guid>
		<description>I have a problem I&#039;d like to tell you about : it&#039;s about jpa, entity, modification, undo (cancel modification)...

A) The context :
Hi, for my project, I used Seam and its Seam Managed Persistence Context. Although I know this context is designed for Conversation scopes, I did not use them at all (all my application is Session scoped).

B) The problem
 I had to make forms for different elements. Let us consider the &quot;Country form&quot;. I had to fill the form (name, currency...), view it &lt;b&gt;and modify&lt;/b&gt; it. However, while modifying the form, I had to be able to perform a classic undo (using a hit on my cancel button) and then retrieve the state of my country as it was before I began doing updates...

C) The unsuccessful attempts and a first solution :
I just have to say it was a pity for me to do that using JPA. I tried all the methods the entityManager gave me (refresh and find mainly) to retrieve the first state of my country object... unsuccessfully :( Finally I made a clone object of me entity, perform the modifications on it and, when clicking the Validate button, re-copy the clone fields onto the real persisted object. 
The clone technic drawbacks :
- The mechanic is quite simple but boring to implement...
- Easy to implement when you have &quot;hello world&quot; sample with 3 or 4 entities living quite independently from each other. But it&#039;s a pity to maintain when you have dozens of entities all of them being part or collection of the other... 

D) Another solution
So although it works, it was a pain to code and maintain. Taken good resolutions for this new year, I searched for other methods and found another one : try to detach the entity using the &quot;evicts&quot; method provided by Hibernate (which is the provider I used). No more clone here : you use directly the object from the database BUT you cut temporarily the connection between the database and Java. Doing so, I can finally persist the changes... or not (undo)! Great but... I see 2 drawbacks using this method :
- it is provider dependent (Hibernate in this case, although I suppose the same method may exist for other JPA implementation)
- the code is quite ugly because you have to load all the collections depending on the object BEFORE you perform this detach : if not, be prepare to have some nice LazyInitializationException...

As I said in a previous post, I&#039;m quite new to JPA, so maybe I missed something but I greatly appreciate if someone could tell me whether or not he ever met such problems and what kind of solutions he had.

Thanks again for the great articles.</description>
		<content:encoded><![CDATA[<p>I have a problem I&#8217;d like to tell you about : it&#8217;s about jpa, entity, modification, undo (cancel modification)&#8230;</p>
<p>A) The context :<br />
Hi, for my project, I used Seam and its Seam Managed Persistence Context. Although I know this context is designed for Conversation scopes, I did not use them at all (all my application is Session scoped).</p>
<p>B) The problem<br />
 I had to make forms for different elements. Let us consider the &#8220;Country form&#8221;. I had to fill the form (name, currency&#8230;), view it <b>and modify</b> it. However, while modifying the form, I had to be able to perform a classic undo (using a hit on my cancel button) and then retrieve the state of my country as it was before I began doing updates&#8230;</p>
<p>C) The unsuccessful attempts and a first solution :<br />
I just have to say it was a pity for me to do that using JPA. I tried all the methods the entityManager gave me (refresh and find mainly) to retrieve the first state of my country object&#8230; unsuccessfully <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  Finally I made a clone object of me entity, perform the modifications on it and, when clicking the Validate button, re-copy the clone fields onto the real persisted object.<br />
The clone technic drawbacks :<br />
- The mechanic is quite simple but boring to implement&#8230;<br />
- Easy to implement when you have &#8220;hello world&#8221; sample with 3 or 4 entities living quite independently from each other. But it&#8217;s a pity to maintain when you have dozens of entities all of them being part or collection of the other&#8230; </p>
<p>D) Another solution<br />
So although it works, it was a pain to code and maintain. Taken good resolutions for this new year, I searched for other methods and found another one : try to detach the entity using the &#8220;evicts&#8221; method provided by Hibernate (which is the provider I used). No more clone here : you use directly the object from the database BUT you cut temporarily the connection between the database and Java. Doing so, I can finally persist the changes&#8230; or not (undo)! Great but&#8230; I see 2 drawbacks using this method :<br />
- it is provider dependent (Hibernate in this case, although I suppose the same method may exist for other JPA implementation)<br />
- the code is quite ugly because you have to load all the collections depending on the object BEFORE you perform this detach : if not, be prepare to have some nice LazyInitializationException&#8230;</p>
<p>As I said in a previous post, I&#8217;m quite new to JPA, so maybe I missed something but I greatly appreciate if someone could tell me whether or not he ever met such problems and what kind of solutions he had.</p>
<p>Thanks again for the great articles.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-93384</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Sat, 05 Dec 2009 14:51:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-93384</guid>
		<description>@Oscar Calderon: Sorry for not replying earlier. I see that Jeanne Boyarsky has already &lt;a href=&quot;http://www.coderanch.com/t/472848/EJB-Other-Java-EE-Technologies/java/Difference-between-detached-entity-new&quot; rel=&quot;nofollow&quot;&gt;answered your question over at CodeRanch&lt;/a&gt;. ;-)

But I&#039;ll answer your question for other readers of this blog: the difference is that a new entity is one that has just been created in Java but has not been persisted in the database yet, while a detached entity exists in Java &lt;em&gt;and&lt;/em&gt; in the database but the Java entity is no longer attached to a session so any updates to it will &lt;em&gt;not&lt;/em&gt; be reflected in the database.</description>
		<content:encoded><![CDATA[<p>@Oscar Calderon: Sorry for not replying earlier. I see that Jeanne Boyarsky has already <a href="http://www.coderanch.com/t/472848/EJB-Other-Java-EE-Technologies/java/Difference-between-detached-entity-new" rel="nofollow">answered your question over at CodeRanch</a>. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>But I&#8217;ll answer your question for other readers of this blog: the difference is that a new entity is one that has just been created in Java but has not been persisted in the database yet, while a detached entity exists in Java <em>and</em> in the database but the Java entity is no longer attached to a session so any updates to it will <em>not</em> be reflected in the database.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oscar Calderon</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-93236</link>
		<dc:creator>Oscar Calderon</dc:creator>
		<pubDate>Fri, 27 Nov 2009 21:49:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-93236</guid>
		<description>Hi, i have a big doubt about some key concepts about JPA. What&#039;s the difference between a detached entity and a new entity?

Regards.</description>
		<content:encoded><![CDATA[<p>Hi, i have a big doubt about some key concepts about JPA. What&#8217;s the difference between a detached entity and a new entity?</p>
<p>Regards.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Constantine</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-93032</link>
		<dc:creator>Constantine</dc:creator>
		<pubDate>Sat, 07 Nov 2009 19:09:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-93032</guid>
		<description>Thank you for a comprehensive article!

In our scenario the data comes from an external source in XML format (we use JAXB to unmarshal it), and needs to be merged with the one already in the DB. We use entityManager.merge in all cases. 

In fact, the major problem is the transfer protocol - the data is coming as a complex deeply nested XML (think of the complete invoice, including information about the customer), so the DIY merge is problematic. We have chosen to specify the meaningful IDs in our entity beans and now it works completely transparently. The major problem was with the compound IDs and complex relationships between entities (and both simultaneously), but those were sorted eventually... Though, at this stage there&#039;s a high risk for application to lose its independence from the JPA provider (for example, ours now works with OpenJPA only).

To speed up the development we successfully use HyperJAXB 3 (http://confluence.highsource.org/display/HJ3/Home), which generates 90% of the code, though still need to handcraft some JPA annotations anyway.</description>
		<content:encoded><![CDATA[<p>Thank you for a comprehensive article!</p>
<p>In our scenario the data comes from an external source in XML format (we use JAXB to unmarshal it), and needs to be merged with the one already in the DB. We use entityManager.merge in all cases. </p>
<p>In fact, the major problem is the transfer protocol &#8211; the data is coming as a complex deeply nested XML (think of the complete invoice, including information about the customer), so the DIY merge is problematic. We have chosen to specify the meaningful IDs in our entity beans and now it works completely transparently. The major problem was with the compound IDs and complex relationships between entities (and both simultaneously), but those were sorted eventually&#8230; Though, at this stage there&#8217;s a high risk for application to lose its independence from the JPA provider (for example, ours now works with OpenJPA only).</p>
<p>To speed up the development we successfully use HyperJAXB 3 (<a href="http://confluence.highsource.org/display/HJ3/Home)" rel="nofollow">http://confluence.highsource.org/display/HJ3/Home)</a>, which generates 90% of the code, though still need to handcraft some JPA annotations anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marcell Manfrin</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-92145</link>
		<dc:creator>Marcell Manfrin</dc:creator>
		<pubDate>Wed, 22 Jul 2009 14:48:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-92145</guid>
		<description>I use this:

&lt;pre lang=&quot;java&quot;&gt;
JpaDao {
  public void persist(E entity) {
    if (entity.getId() == null) {
      entityManager.persist(entity);
    } else {
      if (!entityManager.contains(entity)) {
        entityManager.merge(entity);
      }
    }
  }
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I use this:</p>
<pre lang="java">
JpaDao {
  public void persist(E entity) {
    if (entity.getId() == null) {
      entityManager.persist(entity);
    } else {
      if (!entityManager.contains(entity)) {
        entityManager.merge(entity);
      }
    }
  }
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: JPA Implementation Patterns &#171; Fernando Franzini Java Blog</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-92101</link>
		<dc:creator>JPA Implementation Patterns &#171; Fernando Franzini Java Blog</dc:creator>
		<pubDate>Thu, 16 Jul 2009 12:42:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-92101</guid>
		<description>[...] Saving (detached) entities [...]</description>
		<content:encoded><![CDATA[<p>[...] Saving (detached) entities [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-91437</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Thu, 26 Mar 2009 17:07:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-91437</guid>
		<description>&lt;strong&gt;@Alex:&lt;/strong&gt; how does the DIY merge rule out optimistic locking? I guess it depends on how the optimistic locking is implemented, but then I still can&#039;t think of the examples.

As for the fact that I the serialization methods I use (Hessian, AMF/BlazeDS) apparently do not keep associations intact, I guess the problem here is that I am using domain objects as data transfer objects. That is a practice encouraged by the fact that you get away with in a lot of cases thanks to stuff like the &lt;a href=&quot;http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/servlet/ModelAndView.html&quot; rel=&quot;nofollow&quot;&gt;ModelAndView&lt;/a&gt; and the &lt;a href=&quot;http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/bind/WebDataBinder.html&quot; rel=&quot;nofollow&quot;&gt;WebDataBinder&lt;/a&gt; in Spring Web MVC.

When you use associations (especially ones that would make you walk the entire object graph) you usually do not want them all to be serialized, leaving you with this problem.

In fact I actually write separate DTO&#039;s for my more complex domain models and then you always have manually copy/merge the received data to your domain object.</description>
		<content:encoded><![CDATA[<p><strong>@Alex:</strong> how does the DIY merge rule out optimistic locking? I guess it depends on how the optimistic locking is implemented, but then I still can&#8217;t think of the examples.</p>
<p>As for the fact that I the serialization methods I use (Hessian, AMF/BlazeDS) apparently do not keep associations intact, I guess the problem here is that I am using domain objects as data transfer objects. That is a practice encouraged by the fact that you get away with in a lot of cases thanks to stuff like the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/servlet/ModelAndView.html" rel="nofollow">ModelAndView</a> and the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/bind/WebDataBinder.html" rel="nofollow">WebDataBinder</a> in Spring Web MVC.</p>
<p>When you use associations (especially ones that would make you walk the entire object graph) you usually do not want them all to be serialized, leaving you with this problem.</p>
<p>In fact I actually write separate DTO&#8217;s for my more complex domain models and then you always have manually copy/merge the received data to your domain object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Snaps</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-91421</link>
		<dc:creator>Alex Snaps</dc:creator>
		<pubDate>Tue, 24 Mar 2009 12:41:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-91421</guid>
		<description>Wouldn&#039;t that rule out optimistic locking? Or at least be very intrusive about it&#039;s handling (i.e. manual vs. handled by the provider).
Besides this behavior is related to the &quot;Serialization&quot; method you name in the article, as, apparently, these do not honor the Serialization process of Java. Should they (and I know Hessian does not!), uninitialized associations would remain proxies and be handled by the provider accordingly.
I have the honest impression this results more in fighting the framework, than making it work for you...</description>
		<content:encoded><![CDATA[<p>Wouldn&#8217;t that rule out optimistic locking? Or at least be very intrusive about it&#8217;s handling (i.e. manual vs. handled by the provider).<br />
Besides this behavior is related to the &#8220;Serialization&#8221; method you name in the article, as, apparently, these do not honor the Serialization process of Java. Should they (and I know Hessian does not!), uninitialized associations would remain proxies and be handled by the provider accordingly.<br />
I have the honest impression this results more in fighting the framework, than making it work for you&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Merijn Vogel</title>
		<link>http://blog.xebia.com/2009/03/23/jpa-implementation-patterns-saving-detached-entities/comment-page-1/#comment-91418</link>
		<dc:creator>Merijn Vogel</dc:creator>
		<pubDate>Tue, 24 Mar 2009 11:15:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=914#comment-91418</guid>
		<description>Ah, leave my previous comment out, it was some other error that caused the observed behaviour.</description>
		<content:encoded><![CDATA[<p>Ah, leave my previous comment out, it was some other error that caused the observed behaviour.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
