title: How to check whether a record exists before inserting
body: <p>I am using Hibernate 4.3.6 Final and MySQL 5.5.37.</p>
  
  <p>Suppose I have two tables <code>Book</code> and <code>Words</code> and some code that parses the words in the book and adds them to the <code>Words</code> table. There will certainly be words that appear in more than one book. However, I don't want a new entry for a word if it is already in the database. The only question is if Hibernate is smart enough to have some sort of functionality for this?</p>
  
  <p>There are a couple of solutions, but none of them is pretty:</p>
  
  <ol>
  <li>Check before creating the any <code>Word</code> object if there is already one like it in the database. Horrible for performance.</li>
  <li>Catch the exception that is thrown when trying to insert a duplicate and handle that. Horrible practice.</li>
  <li>Have some sort of static index of the words in the database, which is updated periodically, and search for the word in the list. Maybe slightly faster, but still horrible in terms of performance and memory.</li>
  </ol>
  
  <p>Is there a better approach?</p>