title: Batch update returned unexpected row count from update (Spring/Hibernate)
body: <p>Right now I have 3 tables in my database - Booking, Restaurant and RestaurantTable. I have a one to many mapping between Restaurant and RestaurantTable (a restaurant can have many tables, but a table can have only one restaurant). I have a file called "newTable.jsp" that inserts new tables to the restaurant. But when I try to do that it gives me the following error:</p>
  
  <pre><code>Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
  </code></pre>
  
  <p>I think it's trying to access a RestaurantTable that does not exist yet? But I don't know how to fix that.</p>
  
  <p>Here is my "Restaurant.java" class:</p>
  
  <pre><code>@Entity
  @Table(name="restaurant")
  public class Restaurant {
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      private Long id;
  
      @Column(name="restaurant_name")
      private String restaurantName;
  
      @Column(name="address")
      private String address;
  
      @OneToMany(mappedBy="restaurant", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      private Set&lt;RestaurantTable&gt; table;
  
      // Getters and setters
  </code></pre>
  
  <p>My "RestaurantTable.java":</p>
  
  <pre><code>@Entity
  @Table(name="restaurant_table")
  public class RestaurantTable {
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      private Long id;
  
      @Column(name="table_size")
      private int tableSize;
  
      @Column(name="table_number")
      private int tableNumber;
  
      @ManyToOne
      @JoinColumn(name="restaurant_id")
      private Restaurant restaurant;
  
      // Getters and setters
  </code></pre>
  
  <p>My "newTable.jsp"</p>
  
  <pre><code>&lt;body&gt;
  &lt;jsp:include page="../fragments/menu.jsp"/&gt;
  &lt;div id="body"&gt;
      &lt;section class="content-wrapper main-content clear-fix"&gt;
  
          &lt;h2&gt;Add New Table&lt;/h2&gt;
  
          &lt;form:form method="POST" commandName="table" modelAttribute="table"&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Table size:&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="tableSize" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Table number:&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="tableNumber" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td colspan="3"&gt;&lt;input type="submit" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/form:form&gt;
  
      &lt;/section&gt;
  &lt;/div&gt;
  &lt;jsp:include page="../fragments/footer.jsp"/&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>My RestaurantTableController:</p>
  
  <pre><code>@Controller
  public class RestaurantTableController {
  
  
      @Autowired
      private RestaurantService restaurantService;
  
      @Autowired
      private RestaurantTableService restaurantTableService;
  
      @RequestMapping(value="restaurant/table/{id}", method = RequestMethod.GET)
      public String addRestaurantTable(Model model) {
          model.addAttribute("table", new RestaurantTable());
          return "newTable";
      }
  
      @RequestMapping(value = "restaurant/table/{id}", method = RequestMethod.POST)
      public String addRestaurantTable(@PathVariable Long id, @ModelAttribute ("table") RestaurantTable table) {
          // Get a Restaurant object and add the table to it.
          Restaurant restaurant = restaurantService.getRestaurant(id);
          Set&lt;RestaurantTable&gt; tableSet = restaurant.getTable();
          tableSet.add(table);
          restaurant.setTable(tableSet);
          restaurantService.updateRestaurant(restaurant);
          return "editRestaurant";
      }
  
  }
  </code></pre>
  
  <p>The {id} in RestaurantTableController is a Restaurant id, it is passed along from "editRestaurant.jsp". Any help is appreciated. </p>
  
  <p>EDIT: My updateRestaurant method:</p>
  
  <pre><code>@Override
  public void updateRestaurant(Restaurant restaurant) {
      Session session = this.sessionFactory.getCurrentSession();
      session.update(restaurant);
      logger.info("Restaurant record updated successfully, Restaurant Details=" + restaurant);
  }
  </code></pre>