title: Why does hibernate execute redundant update statements when no values for those tables were changed?
body: <p>I have the following piece of code:</p>
  
  <pre><code>public int makePayment(User forUser, Collection&lt;Movie&gt; cartItems, 
       Address deliverTo) 
  {
     int returnCode = 0;
  
     LOGGER.debug("Number of items in cart - {}", cartItems.size());
  
     Rental rental = new Rental();
     rental.setRentalID(RandomStringUtils.randomAlphanumeric(ID_LENGTH).toUpperCase());
     rental.setUser(forUser); // Update reference to parent
     rental.setAddress(deliverTo);
  
     // Each rental has its own set of rented movies
     Set&lt;RentedMovie&gt; rentedMovies = new HashSet&lt;&gt;(cartItems.size());
     for (Iterator&lt;Movie&gt; iterator = cartItems.iterator(); iterator.hasNext();)
     {
        Movie movie = iterator.next();
        RentedMovie rentedMovie = new RentedMovie();
        rentedMovie.setMovie(movie);
        rentedMovie.setRental(rental); // Update reference to parent
        rentedMovies.add(rentedMovie);
     }
  
     rental.setRentedMovies(rentedMovies);
     rentalDAO.create(rental);
  
     Set&lt;Rental&gt; userRentals = userDAO.findById(forUser.getUserID()).getRentals();
     userRentals.add(rental); // Add to existing set of rentals
  
     try
     {
        String addressID = addressDAO.create(deliverTo);
        LOGGER.debug("Delivery Address successfully inserted. Generated address ID - {}", addressID);
  
        forUser.setRentals(userRentals);
        userDAO.update(forUser); // Internally calls session.merge()
     }
     catch (Exception e) {
        returnCode = -1;
        LOGGER.error("Exception during payment - {}", e.getMessage(), e);
     }
  
     return returnCode;
  }
  </code></pre>
  
  <p>Now when the above method completes, hibernate performs certain <strong><em>redundant</em></strong> update queries as described below:</p>
  
  <p>Let's say I add 2 items to cart, so...</p>
  
  <pre><code>DEBUG PaymentService - Number of items in cart - 2
  </code></pre>
  
  <p>Next, a row is inserted in <code>RENTALS</code> table (expected)</p>
  
  <pre><code>insert 
  into
    rentals
    (bookingDate, userID, addressID, rentalID) 
  values
    (?, ?, ?, ?)
  </code></pre>
  
  <p><code>USER</code> and <code>ADDRESS</code> table updated once since 1 row inserted in <code>RENTALS</code> (redundant)</p>
  
  <pre><code>update
    users 
  set
    userName=?,
    password=?,
    firstName=?,
    lastName=?,
    email=?,
    addressID=? 
  where
    userID=?
  
  update
    addresses 
  set
    line1=?,
    line2=?,
    city=?,
    state=?,
    pincode=? 
  where
    addressID=?
  </code></pre>
  
  <p><code>MOVIES</code> table updated as many times as the number of items in cart (2 in this case, again redundant)</p>
  
  <pre><code>update
    movies 
  set
    movieName=?,
    releaseYear=?,
    rating=?,
    nCopies=?,
    imageUri=?,
    plotLine=? 
  where
    movieID=?
  
  /* And another query with different movieID */
  </code></pre>
  
  <p>2 records inserted in <code>RENTED_MOVIE</code> table (expected)</p>
  
  <pre><code>insert 
  into
    rented_movie
    (rentalID, returnDate, movieID) 
  values
    (?, ?, ?)
  
  /* And one more with same rentalID and diff. movieID */
  </code></pre>
  
  <p>And last, but not the least, a <em>select</em> query to <code>RENTALS</code> table (Why??)</p>
  
  <p>What is the reason for the above mentioned redundant queries?</p>
  
  <p>I am using Hibernate 4.2.1 with MySQL 5.5</p>
  
  <hr>
  
  <p>Link to <a href="http://i275.photobucket.com/albums/jj291/nirav_92/schema-1.png" rel="nofollow">Schema Diagram</a></p>
  
  <p>Link to <a href="http://pastebin.com/5ZWyH5UK" rel="nofollow">User.hbm.xml</a></p>
  
  <p>Link to <a href="http://pastebin.com/9dNWxPjK" rel="nofollow">Rental.hbm.xml</a></p>
  
  <hr>
  
  <p><strong>UPDATE</strong>: It seems no matter what possible combinations of <code>inverse</code> I try, I can't seem to avoid these n+2 updates (n = number of movies in cart) :(</p>
  
  <p>Could someone please let me know if this is a known behavior? If yes, what is the possible workaround?</p>