简体   繁体   English

合并同一实体的多个表示(休眠)

[英]Multiple representations of the same entity are being merged (Hibernate)

I have two tables in my database - Restaurant and RestaurantTable. 我的数据库中有两个表-Restaurant和RestaurantTable。 There is a @OneToMany relationship between them (a restaurant can have many tables). 它们之间有一个@OneToMany关系(一家餐馆可以有很多桌子)。 I am trying to add a table to my restaurant. 我正在尝试向我的餐厅添加一张桌子。 I can add the first table, but as soon as I try to add another table it gives me the following error: 我可以添加第一个表,但是一旦我尝试添加另一个表,就会出现以下错误:

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]

This is how my database looks like: 这是我的数据库的样子:

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=6 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=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

My Restaurant.java file: 我的Restaurant.java文件:

@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)
    @JoinColumn(name="restaurant_id")
    private List<RestaurantTable> table = new ArrayList<RestaurantTable>();

    // Getters and setters

RestaurantTable.java: RestaurantTable.java:

@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

My RestaurantTableController.java: 我的RestaurantTableController.java:

@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, @PathVariable Long id) {
        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) {
        Restaurant restaurant = restaurantService.getRestaurant(id);
        table.setRestaurant(restaurant);
        restaurant.getTable().add(table);
        restaurantService.updateRestaurant(restaurant);
        return "redirect:/bookings";
    }

}

And my updateRestaurant() method in RestaurantDaoImpl.java: 还有我在RestaurantDaoImpl.java中的updateRestaurant()方法:

@Override
public void updateRestaurant(Restaurant restaurant) {
    Session session = this.sessionFactory.getCurrentSession();
    session.merge(restaurant);
    logger.info("Restaurant record updated successfully, Restaurant Details=" + restaurant);
}

Any help is appreciated! 任何帮助表示赞赏!

EDIT: The Hibernate version I am using is 4.3.6. 编辑:我使用的Hibernate版本是4.3.6。

Also, changed my Restaurant table declaration to: 另外,将我的餐厅表声明更改为:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER , mappedBy = "restaurant")
private List<RestaurantTable> table = new ArrayList<RestaurantTable>();

And it gives me the same error as before. 它给了我和以前一样的错误。

EDIT: I read that one solution for that is to change Cascade.ALL to not include MERGE, so the code would look like this: 编辑:我读到的一种解决方案是将Cascade.ALL更改为不包括MERGE,因此代码如下所示:

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.EAGER , mappedBy = "restaurant")
private List<RestaurantTable> table = new ArrayList<RestaurantTable>();

It doesn't give me an error, but it just doesn't do anything, nothing is changing. 它没有给我一个错误,但是它什么也没做,什么都没有改变。

Your Restaurant -> RestaurantTable relationship is being managed on both sides. 您的餐厅-> RestaurantTable关系正在双方管理。 Only one side of the relationship can be the owner of the relationship. 关系的只有一方可以是关系的所有者。 In this case the RestaurantTable should be the owner of the relationship since its table will have the foreign key to the other table. 在这种情况下,RestaurantTable应该是关系的所有者,因为其表将具有指向另一个表的外键。

You need to change the Restaurant table property to indicate that the relationship is being managed by RestaurantTable restaurant property. 您需要更改Restaurant table属性以指示该关系由RestaurantTable restaurant属性管理。 JPA will now use that property for persisting. JPA现在将使用该属性进行持久化。

Change the property declaration to 将属性声明更改为

 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER , mappedBy = "restaurant")
 private List<RestaurantTable> table = new ArrayList<RestaurantTable>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 冬眠,春天。 正在合并同一实体的多个表示 - Hibernate, Spring. Multiple representations of the same entity are being merged 同一实体的多种表示形式正在被合并 - Multiple representations of the same entity are being merged java.lang.IllegalStateException:正在合并同一实体 [] 的多个表示。 分离:[]; 分离:[] - java.lang.IllegalStateException: Multiple representations of the same entity [] are being merged. Detached: []; Detached: [] Spring Boot中同一实体的多种表示 - Multiple representations of same entity in spring boot java.lang.IllegalStateException:具有@ManyToMany 3 个实体的同一实体的多个表示 - java.lang.IllegalStateException: Multiple representations of the same entity with @ManyToMany 3 entities org.springframework.dao.InvalidDataAccessApiUsageException:同一实体的多种表示 - org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same entity Java:同一对象的多种表示形式 - Java: Multiple representations of the same object 同一对象的两种表示形式-休眠问题 - Two Representations of the Same Object - Hibernate Issue Hibernate异常; 找到了同一个集合的两个表示 - Hibernate exception; found two representations of the same collection HIbernate:找到同一集合的两个表示 - HIbernate: Found two representations of the same collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM