title: Hibernate merge vs. persist. Were there changes?
body: <p>I had an app with the following code working just fine until I upgraded hibernate (5.3.2 to 5.4.10 )</p>
  
  <pre><code>    List&lt;UserRole&gt; roles = entity.getRoles();
      for(UserRole r : roles) {
          Em.get().remove(r);
      }
      roles.clear();
  
      for(RoleEnum r : selectedRoles) {
          UserRole role = new UserRole(entity, r);
          Em.get().persist(role);
      }
  
      Em.get().merge(entity);
      Em.get().flush();
  </code></pre>
  
  <p>So, then I started getting an exception </p>
  
  <blockquote>
    <p>Caused by: org.hibernate.TransientPropertyValueException: object
    references an unsaved transient instance - save the transient instance
    before flushing : WEBPIECESxPACKAGE.base.libs.UserRole.user ->
    WEBPIECESxPACKAGE.base.libs.UserDbo</p>
  </blockquote>
  
  <p>This would happen when I 'add' a new user entity.  If I edit an old user(it uses the same exact code), then it would be fine.  </p>
  
  <p>I changed to Em.get().persist(entity) instead and that works for adding a new entity to DB and for editing an old one.  </p>
  
  <p>BUT the documentation still says what old JPA/hibernate used to do for persist which is</p>
  
  <blockquote>
    <p>@throws EntityExistsException if the entity already exists.</p>
  </blockquote>
  
  <p>Is everyone using persist now as the add or edit function?  (ie. having one function that saves or edits as I don't really care which is very very nice AND hibernate can tell from the DB id existing or not whether it is an add or an edit so there is no reason to not have a single call for both).</p>
  
  <p><strong>I am NOW using em.persist() which is working for UPDATE or SAVE...weird</strong></p>
  
  <p>It can be seen on line 110 here
  <a href="https://github.com/deanhiller/webpieces/blob/master/webserver/webpiecesServerBuilder/templateProject/WEBPIECESxAPPNAME/src/main/java/webpiecesxxxxxpackage/web/crud/CrudUserController.java" rel="nofollow noreferrer">https://github.com/deanhiller/webpieces/blob/master/webserver/webpiecesServerBuilder/templateProject/WEBPIECESxAPPNAME/src/main/java/webpiecesxxxxxpackage/web/crud/CrudUserController.java</a></p>
  
  <p>I am using Hibernate 5.4.10</p>
  
  <p>thanks,
  Dean</p>