<?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>Cake Solutions Team Blog &#187; cake solutions</title>
	<atom:link href="http://www.cakesolutions.net/teamblogs/tag/cake-solutions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cakesolutions.net/teamblogs</link>
	<description>void magic() { }</description>
	<lastBuildDate>Tue, 07 Feb 2012 12:02:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Hibernate and primary key unique constaint exception</title>
		<link>http://www.cakesolutions.net/teamblogs/2009/04/23/hibernate-and-primary-key-unique-constaint-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hibernate-and-primary-key-unique-constaint-exception</link>
		<comments>http://www.cakesolutions.net/teamblogs/2009/04/23/hibernate-and-primary-key-unique-constaint-exception/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 10:24:22 +0000</pubDate>
		<dc:creator>Aleksa Vukotic</dc:creator>
				<category><![CDATA[Aleksa's Blog]]></category>
		<category><![CDATA[cake solutions]]></category>
		<category><![CDATA[collections mapping]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[hibernate collection mapping]]></category>
		<category><![CDATA[inverse mapping]]></category>
		<category><![CDATA[list mapping hibernate]]></category>
		<category><![CDATA[open source central]]></category>
		<category><![CDATA[open source centre of excellence]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=194</guid>
		<description><![CDATA[Its one of those things &#8211; you have the same problem every now and then, but not often enough to remember what the solution was. I was implementing some hibernate code, but the tests for it failed due to primary &#8230; <a href="http://www.cakesolutions.net/teamblogs/2009/04/23/hibernate-and-primary-key-unique-constaint-exception/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Its one of those things &#8211; you have the same problem every now and then, but not often enough to remember what the solution was.<br />
I was implementing some hibernate code, but the tests for it failed due to primary key constraint exception (basically hibernate was trying to save already save object using same primary key). I can clearly remember that i have seen this exception before, but the cause and solution were lost somewhere between all those NonUniqueObjectExceptions and jsp exceptions that i had problems with few weeks ago <img src='http://www.cakesolutions.net/teamblogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
So i had to dig into in once more, and promise to myself that i will blog it after i diagnose the problem, so i don&#8217;t forget about it ever again (and save someone else the trouble as well). So here it is:</p>
<p>The domain model and hibernate mapping were really basic &#8211; Descriptor object has reference to List of Note objects:</p>
<pre>
<code>
public class Descriptor{
    private Long id;
    private List&lt;Note&gt; notes = new ArrayList&lt;Note&gt;;

    public void addNote(Note note){
       note.setDescriptor(note);
       this.notes.add(note);
   }
   //getters and setters omitted for clarity
}
public class Note{
    private Long id;
    private String text;
    private Descriptor descriptor;
    //getters and setters omitted for clarity
}
&lt;class name="Descriptor" table="t_descriptor"&gt;
        &lt;id name="id" type="long" unsaved-value="null"&gt;
            &lt;generator class="sequence"&gt;
                &lt;param name="sequence"&gt;s_descriptor_id&lt;/param&gt;
            &lt;/generator&gt;
        &lt;/id&gt;

        &lt;list name="notes" cascade="all"
            &lt;key column="descriptor"/&gt;
            &lt;index column="id"/&gt;
            &lt;one-to-many class="Note"/&gt;
        &lt;/list&gt;
&lt;/class&gt;
&lt;class name="Note" table="t_note"&gt;
        &lt;id name="id" type="long" unsaved-value="null"&gt;
            &lt;generator class="sequence"&gt;
                &lt;param name="sequence"&gt;s_note&lt;/param&gt;
            &lt;/generator&gt;
        &lt;/id&gt;
        &lt;property name="text" column="text" not-null="true"/&gt;
        &lt;many-to-one name="descriptor" column="descriptor" not-null="true"
                     class="Descriptor"/&gt;
    &lt;/class&gt;
</code>
</pre>
<p>Looks simple, but when i run the test for the code above, i got dreaded primary key unique constraint exception.<br />
After a bit of though, i was able to kick myself for not noticing the problem:<br />
The notes property of Descriptor class is mapped with cascade=&#8221;all&#8221; meaning all save, updates, deletes with apply for the child objects as well. However, the inverse is set to false (inverse property is missing, defaults to inverse=&#8221;false&#8221;) &#8211; making both sides of the bi-directional relationship responsible of taking care of the relationship. SO Hibernate generates two insert statements, one because of cascade=&#8221;all&#8221;, and one as part of inverse=&#8221;false&#8221; rule.<br />
The solutions is to set inverse=&#8221;true&#8221; on notes property mapping &#8211; this will make just one side of bi-directional relationship responsible for relationship, and the Hibernate will issue just one insert statement for the Note object.<br />
Here is the correct piece of mapping:</p>
<pre>
        &lt;list name="notes" cascade="all" <strong>inverse="true"&gt;</strong>
            &lt;key column="descriptor"/&gt;
            &lt;index column="id"/&gt;
            &lt;one-to-many class="Note"/&gt;
        &lt;/list&gt;
</pre>
<p>Huh! Cascade and inverse properties when mapping collections in hibernate simplify the development, and improve performance if used correctly, but beware of the pitfalls of unsuspected exception.<br />
To read more go to the Hibernate website or read this blog: <a href="http://www.codeweblog.com/hibernate-in-the-inverse-and-cascade/">http://www.codeweblog.com/hibernate-in-the-inverse-and-cascade/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2009/04/23/hibernate-and-primary-key-unique-constaint-exception/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

