首页 > hibernate manytomany 更新主表字段,从表关系被删除

hibernate manytomany 更新主表字段,从表关系被删除

hibernate 实体之间存在多对多关系。例如:

public class user{
 private int id;
 private String name;
 @ManytoMany(fetch=FetchType.LAZY)
 @JoinTable(name="tb_user_role",joinColumns={@JoinColumn(name="user_id")},
    inverseJoinColumns={@JoinColumn(name="role_id")})
 private List<Role> roles; 
}
public class role{
    private int id;
    private String name;
}

用户一方关联了角色,我通过id值load出某一条user对象(此user对象关联了2条role对象),信息展现在页面上后,session被关闭,user成为游离态,现在我修改了user的名字,进而执行update操作(update时user对象中只有user的基本信息,没有保存role关联信息)。
问题来了:
update后,此user对象关联的两条role对象的关联关系被删除了,怎么做才能保证关联关系不被删除。
我一一试了cascadeType的属性都不起作用,对cascadeType不太清楚,希望懂的人能详细解释一下,谢谢!

【热门文章】
【热门文章】