<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Supple software comments on Messing with Hibernate IDs</title>
    <link>http://supplesoftware.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Supple software comments</description>
    <item>
      <title>"Messing with Hibernate IDs" by petrovg</title>
      <description>&lt;p&gt;What happens if you have a class with auto-generated ID, but you also want to have some of the ID values hard-coded? A mess.&lt;/p&gt;

&lt;p&gt;Say I have a class Currency, with an ID , which is auto-generated, but I want (the reason is irrelevant at the moment and not subject of discussion) USD and JPY to have IS 1 and 2, respectively. So, I have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/p&gt;

&lt;pre&gt;
Currency jpy = new Currency(2, "JPY");
hibernateTemplate.saveOrUpdate(jpy);
&lt;/pre&gt;

&lt;p&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Well, that&amp;#8217;s going to cause you some trouble. If USD is already in the DB with the ID of 2, this will work. If, however it is not, it will throw an Exception, saying that:&lt;/p&gt;

&lt;p&gt;Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1&lt;/p&gt;

&lt;p&gt;Quite annoying, but understandable - it ignores my ID, assigns it another ID, and then tries to issue an update for that new ID, which of course returns nothing, or even worse, updates some other record, which happens to have the same id.&lt;/p&gt;

&lt;p&gt;Personally, I wouldn&amp;#8217;t mix them.&lt;/p&gt;

</description>
      <pubDate>Thu, 17 May 2007 04:46:03 PDT</pubDate>
      <guid>&lt;a href="/articles/2007/05/17/messing-with-hibernate-ids"&gt;Messing with Hibernate IDs&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2007/05/17/messing-with-hibernate-ids"&gt;Messing with Hibernate IDs&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
