title: Constraintviolation on a nonmodified collection/entity
body: <p>For the past few days I've been banging my head against a very frustrating problem.</p>
  
  <p>We have a large number of hibernate entities which is all generated code. That means that following @OneToMany collections(among other things) are created:</p>
  
  <pre><code>@OneToMany(cascade = CascadeType.ALL)
  @JoinTable(name = "tablename")
  private java.util.List&lt;classname&gt; fieldname;
  </code></pre>
  
  <p>In our application we want to be able to copy all this for a second user. We do this by reading all those classes and fields and recreating them later(via their constructors). It seems however that reading this collection causes Hibernate to mark the collection as dirty. This later leads to a strange constraint violation when writing to the database when Hibernate first tries to delete a row and then insert it again. </p>
  
  <p>My problem is this:
  Why does Hibernate even want to delete and then insert that row? And why does it fail? </p>
  
  <p>My guess is that Hibernate tries to recreating the entire collection because for some reason Hibernate thinks it is modified. However, this is clearly not the case.</p>
  
  <p>Edit: I should point out that using HibernateSession#evict after reading the collection seems to fix the issue. However, this feels very hacky and I prefer a more thoughtout one.</p>