2013-07-29 72 views
0

我剛剛使用spring PropertyEditors從我的web表單中設置了id的綁定,並注意到M:M記錄正在被刪除並重新插入。起初我以爲這是因爲我使用CustomCollectionEditor而發生的,但UserEditor也是如此。hibernate @ManyToMany刪除並重新插入

我的域對象都具有使用主鍵的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; 
} 

任何方式我可以得到綁定工作與休眠的好?我喜歡將經理更改包含在表單提交中。

回答

0

使用轉換服務解決了這個問題,因爲它轉換每個實體單獨而不是集合本身。