<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>simonpena.com &#187; Hibernate</title>
	<atom:link href="http://simonpena.com/blog/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://simonpena.com/blog</link>
	<description>Una mezcla heterogénea de tecnología y desvaríos</description>
	<lastBuildDate>Tue, 11 Oct 2011 19:14:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Misunderstanding Hibernate</title>
		<link>http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=misunderstanding-hibernate</link>
		<comments>http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 18:30:40 +0000</pubDate>
		<dc:creator>Simón</dc:creator>
				<category><![CDATA[Pennybank]]></category>
		<category><![CDATA[CascadeType]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://pennybank.wordpress.com/?p=45</guid>
		<description><![CDATA[After some more tests, I've found that I was wrong with some ideas I had about Hibernate.<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/' addthis:title='Misunderstanding Hibernate '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>These last days I've been doing more <strong>JUnit</strong> tests. And thanks to them, I've discovered some serious misunderstanding I had about <strong>Hibernate</strong>, <strong>Cascading</strong> (yes, again) and <strong>ManyToOne</strong>, <strong>OneToMany</strong> relationships.</p>
<p>The first mistake I have was start coding too fast. I started following a tutorial, but I confess that I focused too much on code, an little on the theory. Following that tutorial I set a <strong>OneToMany</strong> relationship between an <em>user</em> and his <em>accounts</em>. Somehow, I thought that the <em>account</em> list in the <em>user</em> side would get filled <a title="automagically - Wiktionary" href="http://en.wiktionary.org/wiki/automagically" target="_blank">automagically</a>, driven by the annotations I was using. As the accounts got dropped from the database when I deleted an user, I thought everything was alright, and I didn't think about too much.</p>
<p>Yesterday, doing some tests on the <em>category</em> entity, I found that in a child-parent relationship, the child didn't have its parent <em>category</em> updated when I deleted that parent <em>category</em>. In the database I had set <strong>"ON DELETE SET NULL"</strong>, and looking at the database while performing the tests showed that it was working there. But <strong>Hibernate</strong> didn't seem aware of that behavior, and the link to the old parent <em>category</em> still appeared in the child. That led me, after some tries, to understand how cascading was supposed to work and how badly I was using it. I had several cascading annotations in the <strong>ManyToOne</strong> side of some relationships, which meant that when I made a change on the <em>many</em> side, the other side was getting affected. And that wasn't the behavior I was expecting!</p>
<p>I went to the documentation to learn how should annotations work, and what did they mean according to the context they were used in. After that, I removed most of my annotations, and made some changes, in the <em>update</em> and <em>delete</em> methods in my <em>DAOs</em> to spread the changes when that was needed.</p>
<p>Now, an <em>user</em> has a list of <em>accounts</em>. When an <em>account</em> is created, it sets a reference to the <em>user</em> who has it. And makes that <em>user</em> add it to his <em>account</em> list. When an <em>account</em> is deleted, it makes the <em>user</em> remove it from the <em>account</em> list. As the <em>account</em> <em>operations</em> have a reference to an <em>account</em>, <strong>Hibernate</strong> is responsible to update that reference.</p>
<p>The <em>categories</em> behavior is similar. When a root <em>category</em> is created, it creates an empty list of child <em>categories</em>. When you add a child <em>category</em> to a parent <em>category</em>, you add that child to his parent list. When deleting the parent, you have to go through the list, updating all the references in his children. When deleting a child, you have to remove it from his parent list.</p>
<p>After these changes, everything is working now. I know that some of the tests I've done are too bound to my interpretation of the behavior they have to show, and so they may be wrong. But at least I get rid of part of the errors I still have.</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/' addthis:title='Misunderstanding Hibernate '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate common errors</title>
		<link>http://simonpena.com/blog/proyectos/pennybank/hibernate-common-errors/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hibernate-common-errors</link>
		<comments>http://simonpena.com/blog/proyectos/pennybank/hibernate-common-errors/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 22:43:26 +0000</pubDate>
		<dc:creator>Simón</dc:creator>
				<category><![CDATA[Pennybank]]></category>
		<category><![CDATA[deleted entity passed to persist]]></category>
		<category><![CDATA[detached entity passed to persist]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[id to load is required for loading]]></category>

		<guid isPermaLink="false">http://pennybank.wordpress.com/?p=43</guid>
		<description><![CDATA[A better explanation about some usual Hibernate-related errors: deleted entity passed to persist, detached entity passed to persist and id to load is required for loading<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/hibernate-common-errors/' addthis:title='Hibernate common errors '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Just two days after I <a title="Mind the Cascade" href="http://www.simonpena.com/blog/proyectos/pennybank/mind-the-cascade" target="_blank">blogged</a> <a title="Testing..." href="http://www.simonpena.com/blog/proyectos/pennybank/testing" target="_blank">about</a> the errors <strong>deleted entity passed to persist</strong>, <strong>detached entity passed to persist</strong> and <strong>id to load is required for loading</strong>, some people came here from web searches.</p>
<p>As I'm not sure if those explanations were clear enough, I'll try to synthesise here:</p>
<ul>
<li><strong>Deleted entity passed to persist</strong>. One entity, supposed to be deleted, is still referenced / accessed. In my case, I had a problem with "<em>cascading</em>". The behavior I expected was "<strong>on delete cascade</strong>" and "<strong>on update cascade</strong>". I have those constraints well in the database, but they weren't right in the Hibernate annotations. After I changed that, the error stopped. While I was searching why I was getting this error, I've seen people whose problem was different. They had, let's say, a list of entities. Those entities were related to others, and, when deleting, they had to take care of deleting both the link and the persisted entity. This was the most common scenario of this problem, and is better explained <a title="Ejb3 Tutorial 3 - Finish Conversion" href="http://schuchert.wikispaces.com/Ejb3+Tutorial+3+-+Finish+Conversion" target="_blank">here</a>, for example.</li>
<li><strong>Detached entity passed to persist</strong>. One entity, which is supposed to be up to date but is not, is passed to persist. In my case, I was trying to create an entity (an Operation), related to another (the Account where that Operation was taking place). The persist action was failing because my version of the Account was incorrect. The solution was just to update the entity.</li>
<li><strong>Id to load is required for loading</strong>. Trying to access an entity without giving an actual identifier. In my case this was the easiest problem: I was calling <em>find</em> passing a non initialized long. Passing the correct, initialized values fixed the problem.</li>
</ul>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/hibernate-common-errors/' addthis:title='Hibernate common errors '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://simonpena.com/blog/proyectos/pennybank/hibernate-common-errors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

