hibernate @ManyToMany删除并重新插入

时间:2013-07-29 16:58:17

标签: spring hibernate spring-mvc

我刚刚使用spring PropertyEditors从我的网络表单设置了id的绑定,并注意到M:M记录正被删除并重新插入。起初我以为这是因为我使用的是CustomCollectionEditor,但UserEditor也是如此。

我的域对象都有使用主键的equals / hashcode方法,如果尚未保存对象则为ref。

Hibernate: update delivery_team set name=? where id=?
Hibernate: delete from delivery_team_manager where delivery_team_id=?
Hibernate: delete from delivery_team_resource where delivery_team_id=?
Hibernate: insert into delivery_team_manager (delivery_team_id, user_id) values (?, ?)
Hibernate: insert into delivery_team_manager (delivery_team_id, user_id) values (?, ?)
Hibernate: insert into delivery_team_manager (delivery_team_id, user_id) values (?, ?)
Hibernate: insert into delivery_team_manager (delivery_team_id, user_id) values (?, ?)

我尝试使用单独的回发添加管理器,并且不会删除记录。所以这显然不是一个休眠问题。

@RequestMapping(value = "add_manager/{teamid}/{uid}", method = RequestMethod.GET)
public String addManager(@PathVariable Long teamid, @PathVariable Long uid, Model model) {
    DeliveryTeam team = deliveryTeamService.getById(teamid);
    User user = userService.loadUserById(uid, false);
    team.getManager().add(user);
    deliveryTeamService.save(team);
    return "redirect:/delivery_team/update/" + teamid;
}

任何方式我都可以通过hibernate获得良好的绑定?我想将经理变更纳入表格提交。

1 个答案:

答案 0 :(得分:0)

使用转换服务解决了这个问题,因为它会单独转换每个实体而不是集合本身。