title: Adding rows to a database in a one to many mapping (Hibernate/Spring)
body: <p>EDIT: Still no solution :/</p>
  
  <p>I really need some help with something I have been stuck on for 3 days now. I have a database with two tables, set up like this:</p>
  
  <pre><code>DROP TABLE IF EXISTS `restaurant`;
  /*!40101 SET @saved_cs_client     = @@character_set_client */;
  /*!40101 SET character_set_client = utf8 */;
  CREATE TABLE `restaurant` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `restaurant_name` TEXT,
    `address` TEXT,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  /*!40101 SET character_set_client = @saved_cs_client */;
  
  DROP TABLE IF EXISTS `restaurant_table`;
  /*!40101 SET @saved_cs_client     = @@character_set_client */;
  /*!40101 SET character_set_client = utf8 */;
  CREATE TABLE `restaurant_table` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `table_size` int,
    `table_number` int,
    `restaurant_id` int(11),
    PRIMARY KEY (`id`),
    FOREIGN KEY (`restaurant_id`) references `restaurant`(`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  /*!40101 SET character_set_client = @saved_cs_client */;
  </code></pre>
  
  <p>There is a one to many mapping between those two tables (as in a Restaurant can have many Tables). I am trying to add a Table into my Restaurant, but can't seem to do that (or sometimes I can't add more than one table).</p>
  
  <p>I would really appreciate it if someone could point me in the right direction or help me figure out what exactly is wrong in my code. I have expreminted with different cascade options and updateing instead of merging, but never seem to get it working. I think there might be a fundamental fault in my code, maybe the whole way I'm doing it is wrong?</p>
  
  <p>My Restaurant.java file:</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(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="restaurant")
      private List&lt;RestaurantTable&gt; table = new ArrayList&lt;RestaurantTable&gt;();
  
      // Getters and setters
  </code></pre>
  
  <p>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 RestaurantTableController.java:</p>
  
  <pre><code>@Controller
  public class RestaurantTableController {
  
      @Autowired
      private RestaurantService restaurantService;
  
      @Autowired
      private RestaurantTableService restaurantTableService;
  
      @RequestMapping(value="restaurant/{id}/table", method = RequestMethod.GET)
      public String addRestaurantTable(Model model, @PathVariable Long id) {
          model.addAttribute("table", new RestaurantTable());
          return "newTable";
      }
  
      @RequestMapping(value = "restaurant/{id}/table", method = RequestMethod.POST)
      public String addRestaurantTable(@PathVariable Long id, @ModelAttribute ("table") RestaurantTable table) {
          Restaurant restaurant = restaurantService.getRestaurant(id);
          table.setRestaurant(restaurant);
          restaurant.getTable().add(table);
          restaurantService.mergeRestaurant(restaurant);
          return "redirect:/bookings";
      }
  
  }
  </code></pre>
  
  <p>And my mergeRestaurant() method in RestaurantDaoImpl.java:</p>
  
  <pre><code>@Override
  public void mergeRestaurant(Restaurant restaurant) {
      Session session = this.sessionFactory.getCurrentSession();
      session.merge(restaurant);
      logger.info("Restaurant record updated successfully, Restaurant Details=" + restaurant);
  }
  </code></pre>
  
  <p>The error I get right now is:</p>
  
  <pre><code>HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Multiple representations of the same entity [RestaurantTable#6] are being merged. Detached: [Table number 6]; Detached: [Table number 5]
  </code></pre>
  
  <p>Any help is appreciated!</p>
  
  <p>EDIT:
  Changed</p>
  
  <pre><code>private List&lt;RestaurantTable&gt; table = new ArrayList&lt;RestaurantTable&gt;();
  </code></pre>
  
  <p>to </p>
  
  <pre><code>private Set&lt;RestaurantTable&gt; table = new HashSet&lt;RestaurantTable&gt;();
  </code></pre>
  
  <p>Also, instead of having "CascadeType.ALL" in Restaurant, I moved it to RestaurantTable.java.</p>
  
  <p>Now, if I try to enter the first Table into the Restaurant it gives me this error:</p>
  
  <pre><code>HTTP Status 500 - Request processing failed; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [RestaurantTable#6]
  </code></pre>
  
  <p>Any ideas? </p>