title: Hibernate/Dropwizard: Find or create with @UnitOfWork does not work
body: <p>We have a rest API that we wrote with dropwizard. One functionality of the API is to create event triples of the form: userID - action - itemID
  If there is no action yet for this userID/itemID combination we create a new event. The corresponding resource function has the @UnitOfWork annotation:</p>
  
  <pre><code>@POST
  @UnitOfWork
  @Timed
  public Event createEvent(Event event) {
      return eventDAO.updateOrCreate(event);
  }
  </code></pre>
  
  <p>If this is the first event for this particular userID or itemID we create a user or an item respectively. This is the function for the items (users is the same):</p>
  
  <pre><code>public Item getOrCreate(Event event) {
      Item item = findOne(event.getItemId());
      if(item == null) {
          item = new Item();
          item.setItemId(event.getItemId());
          create(item);
      }
      return item;
  }
  </code></pre>
  
  <p>The problem is that we have duplicate items (i.e. errors if we enforce the itemId to be unique). If we have two requests in separate threads like this</p>
  
  <pre><code>user1 action1 item1
  user2 action2 item1
  </code></pre>
  
  <p>it seems that both try to create one. From the <a href="https://dropwizard.github.io/dropwizard/manual/hibernate.html#transactional-resource-methods" rel="nofollow">documentation</a> of the @UnitOfWork we assumed that everything would be wrapped in a transaction and therefore this shouldn't happen. 
  When we added a database constraint that the itemId should be unique, we get a PSQL exception "duplicate key value violates unique constraint...".</p>
  
  <p>What am I missing?</p>