title: Existence checking in Transaction in Hibernate
body: <p>I am having a similar problem to the man who posted this message in the spring forums: <a href="http://forum.springsource.org/archive/index.php/t-20943.html" rel="nofollow">http://forum.springsource.org/archive/index.php/t-20943.html</a></p>
  
  <p>Basically I am writing an auto-registration feature for a website I work on. If the user exists in a third-party database we are migrating users from and not in our DB then the log in process will automatically create a user in our DB.</p>
  
  <p>To do this I have to first attempt to authenticate the user on log in with the email and password. If that fails I check the third party DB and try to create the user using this information. As in the link above, It seems that this existence check followed by an insert causes the method to be marked by Hibernate as Roll-back only.</p>
  
  <p>I have tried using Transactional Requires_New on the existence check to force it into a new Transaction, I was hoping this transaction would be marked as Roll-back only instead of the parent transaction, but this hasn't worked. </p>
  
  <p>Can anyone advise on how to do this? The link above suggests inserting and using a duplicate key error as the check that the user already exists but this seems messy and would require significant redesign.</p>
  
  <p>btw we are using Hibernate, Spring and Annotations to handle transactions.</p>