<?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: Why did Hibernate update my database?</title>
	<atom:link href="http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/</link>
	<description>Software development done right!</description>
	<lastBuildDate>Wed, 08 Feb 2012 14:41:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Sridhar Sreenivasan</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-117414</link>
		<dc:creator>Sridhar Sreenivasan</dc:creator>
		<pubDate>Wed, 01 Feb 2012 21:06:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-117414</guid>
		<description>Apologise for responding to this old blog. But Iam running into a weird scenario, and came across this article. Possibly facing this issue I guess. The overview of my issue is-
ParentA class has a bi-directional inverse=&quot;true&quot; with a child Map. 
Two threads A, and B process the same ParentA instance, and creates a new Child object instance. 
ThreadA-&gt; ParentA1 -&gt; ChildA1(key1, value1)
ThreadB-&gt; ParentA1 -&gt; ChildA1(key1, value2)

Logic in ParentA is such that if there&#039;s a key existing the value should be updated. Else the new Child object is persisted. We retry on StaleObjectState by restarting the session, and reloading the objects. So when trying in my dev. environmnet I process the same ParentA object across multiple threads. Processing of ThreadA inserts ChildA1, processing of ThreadB throws StaleObject during flush. Retry logic appropriately updates the ChildA to value2. 
But in our production environment ThreadB creates the new ChildA1 object (thus resulting in duplicates). Logs do indicate a StaleObject state. Despite the exception the child object is persisted. Is that related?</description>
		<content:encoded><![CDATA[<p>Apologise for responding to this old blog. But Iam running into a weird scenario, and came across this article. Possibly facing this issue I guess. The overview of my issue is-<br />
ParentA class has a bi-directional inverse=&#8221;true&#8221; with a child Map.<br />
Two threads A, and B process the same ParentA instance, and creates a new Child object instance.<br />
ThreadA-&gt; ParentA1 -&gt; ChildA1(key1, value1)<br />
ThreadB-&gt; ParentA1 -&gt; ChildA1(key1, value2)</p>
<p>Logic in ParentA is such that if there&#8217;s a key existing the value should be updated. Else the new Child object is persisted. We retry on StaleObjectState by restarting the session, and reloading the objects. So when trying in my dev. environmnet I process the same ParentA object across multiple threads. Processing of ThreadA inserts ChildA1, processing of ThreadB throws StaleObject during flush. Retry logic appropriately updates the ChildA to value2.<br />
But in our production environment ThreadB creates the new ChildA1 object (thus resulting in duplicates). Logs do indicate a StaleObject state. Despite the exception the child object is persisted. Is that related?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Hiller</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-94602</link>
		<dc:creator>Steve Hiller</dc:creator>
		<pubDate>Fri, 16 Apr 2010 14:00:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-94602</guid>
		<description>Here&#039;s a good one:

I had to use a stored procedure to read data from a database. Some of the fields are of fixed length of type CHAR. Some of the data in these fields were padded with spaces to fill their fixed lengths. I have an object that is mapped to these fields, each property being of type String. For each setter, I was using the trim method to remove the padding from the input field. Using the trim method caused the object to be marked as dirty, thus causing an unwanted update. Really odd at first since there is no corresponding table to update to.</description>
		<content:encoded><![CDATA[<p>Here&#8217;s a good one:</p>
<p>I had to use a stored procedure to read data from a database. Some of the fields are of fixed length of type CHAR. Some of the data in these fields were padded with spaces to fill their fixed lengths. I have an object that is mapped to these fields, each property being of type String. For each setter, I was using the trim method to remove the padding from the input field. Using the trim method caused the object to be marked as dirty, thus causing an unwanted update. Really odd at first since there is no corresponding table to update to.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alan Bond</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-94010</link>
		<dc:creator>Alan Bond</dc:creator>
		<pubDate>Wed, 27 Jan 2010 20:53:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-94010</guid>
		<description>I know I am responding to an old blog, but I ran into the same issue around January 2009 as well.  Turned out that an attempt to improve efficiency by setting default values in the DTO classes that are mapped to tables will cause an immediate update of the record selected before it is returned.  When log records are part of the return the log data that was saved bloated exponentially!</description>
		<content:encoded><![CDATA[<p>I know I am responding to an old blog, but I ran into the same issue around January 2009 as well.  Turned out that an attempt to improve efficiency by setting default values in the DTO classes that are mapped to tables will cause an immediate update of the record selected before it is returned.  When log records are part of the return the log data that was saved bloated exponentially!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geir Hedemar</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-91496</link>
		<dc:creator>Geir Hedemar</dc:creator>
		<pubDate>Tue, 07 Apr 2009 10:13:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-91496</guid>
		<description>I have had the same problem.

A nice workaround that is available if you also use Spring and annotation-driven transactions is to declare your read-only service calls to be just that. This will flush out any writethroughs early on. The resulting code will look something like

@Transactional(readOnly=true) public void serviceCall(...)

which I also find to be concise and readable.</description>
		<content:encoded><![CDATA[<p>I have had the same problem.</p>
<p>A nice workaround that is available if you also use Spring and annotation-driven transactions is to declare your read-only service calls to be just that. This will flush out any writethroughs early on. The resulting code will look something like</p>
<p>@Transactional(readOnly=true) public void serviceCall(&#8230;)</p>
<p>which I also find to be concise and readable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pawel Kaczor</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-91495</link>
		<dc:creator>Pawel Kaczor</dc:creator>
		<pubDate>Tue, 07 Apr 2009 06:45:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-91495</guid>
		<description>Thanks for explanation. Yes, you are right. Additional select is necessery to enable dirty checking for detached entities. This select is generated automatically if select-before-update=true.</description>
		<content:encoded><![CDATA[<p>Thanks for explanation. Yes, you are right. Additional select is necessery to enable dirty checking for detached entities. This select is generated automatically if select-before-update=true.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maarten Winkels</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-91494</link>
		<dc:creator>Maarten Winkels</dc:creator>
		<pubDate>Tue, 07 Apr 2009 03:04:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-91494</guid>
		<description>@Pavel: By &quot;loaded state&quot; I mean &quot;the state of the object (the values of its properties) when loaded form the database&quot; as mentioned in the second line you copied. It refers to the field: org.hibernate.engine.EntityEntry.loadedState, which contains these values.

The org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(FlushEntityEvent) method has a line, that reads:
&lt;code&gt;cannotDirtyCheck = loadedState==null;&lt;/code&gt;
(It is amazing what reading code can teach you sometimes...)

Now when you reattach an object, the object is not loaded from the database and Hibernate has no way of knowing whether it has changed in the meantime or not (other then querying the database, which you can configure it to do). There are several ways to reattach: using .update() or .lock() or cascading any of these methods. If you .update(), obviously Hibernate will update the database. If you lock, it depends on your LockMode.</description>
		<content:encoded><![CDATA[<p>@Pavel: By &#8220;loaded state&#8221; I mean &#8220;the state of the object (the values of its properties) when loaded form the database&#8221; as mentioned in the second line you copied. It refers to the field: org.hibernate.engine.EntityEntry.loadedState, which contains these values.</p>
<p>The org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(FlushEntityEvent) method has a line, that reads:<br />
<code>cannotDirtyCheck = loadedState==null;</code><br />
(It is amazing what reading code can teach you sometimes&#8230;)</p>
<p>Now when you reattach an object, the object is not loaded from the database and Hibernate has no way of knowing whether it has changed in the meantime or not (other then querying the database, which you can configure it to do). There are several ways to reattach: using .update() or .lock() or cascading any of these methods. If you .update(), obviously Hibernate will update the database. If you lock, it depends on your LockMode.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pawel Kaczor</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-91492</link>
		<dc:creator>Pawel Kaczor</dc:creator>
		<pubDate>Mon, 06 Apr 2009 20:07:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-91492</guid>
		<description>&gt; As mentioned eralier, Hibernate uses the &quot;loaded state&quot; to determine whether an object is dirty. This is the state of the object (the values of its properties) when loaded form the database. Hibernate stores this information in its persistence context. When dirty checking, Hibernate compares these values to the current values. When the &quot;loaded state&quot; is not available, Hibernate effectively cannot do dirty checking and deems the object dirty. The only scenario, however, in which the loaded state is unavailable is when the object has been re-attached to the session and thus not loaded from the database. The process I was looking into, however did not work with detached data.

I don&#039;t know what you mean by &quot;loaded state&quot;, but whatever it is you are wrong saying that dirty checking doesn&#039;t work when entity is reattached. It works, otherwise use of this feature would be very limited.</description>
		<content:encoded><![CDATA[<p>&gt; As mentioned eralier, Hibernate uses the &#8220;loaded state&#8221; to determine whether an object is dirty. This is the state of the object (the values of its properties) when loaded form the database. Hibernate stores this information in its persistence context. When dirty checking, Hibernate compares these values to the current values. When the &#8220;loaded state&#8221; is not available, Hibernate effectively cannot do dirty checking and deems the object dirty. The only scenario, however, in which the loaded state is unavailable is when the object has been re-attached to the session and thus not loaded from the database. The process I was looking into, however did not work with detached data.</p>
<p>I don&#8217;t know what you mean by &#8220;loaded state&#8221;, but whatever it is you are wrong saying that dirty checking doesn&#8217;t work when entity is reattached. It works, otherwise use of this feature would be very limited.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-91486</link>
		<dc:creator>John</dc:creator>
		<pubDate>Mon, 06 Apr 2009 14:35:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-91486</guid>
		<description>&gt; Using Hibernate can save a developer a lot of time,
&gt; when things are running smoothly. When a problem is
&gt; encountered, a lot of specialized Hibernate knowledge
&gt; and a considerable amount of time is often needed to
&gt; diagnose and solve it.

True. True.</description>
		<content:encoded><![CDATA[<p>&gt; Using Hibernate can save a developer a lot of time,<br />
&gt; when things are running smoothly. When a problem is<br />
&gt; encountered, a lot of specialized Hibernate knowledge<br />
&gt; and a considerable amount of time is often needed to<br />
&gt; diagnose and solve it.</p>
<p>True. True.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nico Mommaerts</title>
		<link>http://blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/#comment-91485</link>
		<dc:creator>Nico Mommaerts</dc:creator>
		<pubDate>Mon, 06 Apr 2009 14:25:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1002#comment-91485</guid>
		<description>Omg what a coincidence, we had exactly the same problem today!</description>
		<content:encoded><![CDATA[<p>Omg what a coincidence, we had exactly the same problem today!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.xebia.com/2009/04/06/why-did-hibernate-update-my-database/feed/ ) in 0.52654 seconds, on Feb 9th, 2012 at 8:00 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 9:00 pm UTC -->
