title: Workaround for Spring/Hibernate due to non-standard behaviour of UNIQUE constraint in MS SQL
body: <p>There is a UNIQUE database constraint on an index which doesn't allow more than one record having identical columns.</p>
  
  <p>There is a piece of code, managed by Hibernate (v2.1.8), doing two DAO <BR>
   <code>getHibernateTemplate().save( theObject )</code><BR>
  calls which results two records entered into the table mentioned above.</p>
  
  <p>If this code is executed without transactions, it results INSERT, UPDATE, then another INSERT and another UPDATE SQL statements and works fine. Apparently, the sequence is to insert the record containing DB NULL first, and then update it with the proper data.</p>
  
  <p>If this code is executed under Spring (v2.0.5) wrapped in a single Spring transaction, it results two INSERTS, followed by immediate exception due to UNIQUE constraint mentioned above.</p>
  
  <p>This problem only manifests itself on <a href="http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=299229" rel="nofollow noreferrer">MS SQL</a> due to its incompatibility with ANSI SQL. It works fine on MySQL and Oracle. Unfortunately, our solution is cross-platform and must support all databases.</p>
  
  <p>Having this stack of technologies, what would be your preferred workaround for given problem?</p>