我有一個問題春休眠,當我更新這將刪除相關角色的用戶... 難道是一個級聯的事項?我已經試過各種級聯配置的...春休眠時更新的用戶會刪除其作用
人
@MappedSuperclass
public abstract class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int id;
@OneToOne(cascade = { CascadeType.ALL } )
@JoinTable(name="user_roles",
joinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}
)
.
.
.
}
角色
@Entity
@Table(name="roles")
public class Role {
@Id
@GeneratedValue
private Integer id;
private String authority;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name="user_roles",
joinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")}
)
private Set<User> userRoles;
.
.
.
}
UserRole的
@Entity
@Table (name = "user_roles")
public class UserRole {
@Id
@Column(name = "user_id")
private int userId;
@Column(name = "role_id")
private int roleId;
.
.
.
}
名在UserDAOImpl
@Override
public void updateUser(User user) {
try {
this.sessionFactory.getCurrentSession().update(user);
} catch (HibernateException e) {
System.out.println(e);
}
}
我得到了用戶這樣的(誰已經登錄的):
@RequestMapping(value = "/edit/", method = RequestMethod.GET)
public String editUser(ModelMap map,Principal principal) {
try{
//Get logged in username
String loggedUserEmail = principal.getName();
User user = userService.getUserByEmail(loggedUserEmail);
map.addAttribute("user", user);
} catch(Exception e) {
log.error("/user/edit/---"+e);
}
return "forms/userForm";
}
然後我嘗試提交用戶窗體上的一些變化後更新。
啊,我插入的UserRole這樣的:
//Add userRole to db
UserRole userRole = new UserRole();
userRole.setUserId(user.getId());
userRole.setRoleId(1);
userRoleService.addUserRole(userRole);
您不必在用戶類中提及級聯,如果您不提及級聯,那麼您必須使用不同的Dao實現updateRoles(設置角色)手動添加用戶角色。 –
Zeus