views:

236

answers:

0

i have table users: relation many-to-many with table role, groups

i use hibernate to map with my class;

<hibernate-mapping package="cbs.domain">
    <class name="User" table="users">
    <id name="id" column="id">
        <generator class="native" />
    </id>
    <property name="username" />
    <property name="password" />
    <property name="fullname" />
    <set name="roles" table="user_role" cascade="delete">
        <key column="user_id" />
        <many-to-many column="role_id" class="Role" />
    </set>
    <set name="groups" table="group_members" cascade="delete">
        <key column="user_id" />
        <many-to-many column="group_id" class="Group" />
    </set>
</class>
</hibernate-mapping>

in spring, i have a form to edit user's profile, include: fullname & password (no edit role & group).

but when i save this form, relevant data in table user_role & group_member was lost.

my controller

public class MyProfileController extends BaseFormController {

private UserDao userDao;

public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}

@Override
protected Object formBackingObject(HttpServletRequest request)
        throws Exception {

    String currentUser = SecurityContextHolder.getContext()
            .getAuthentication().getName();

    return userDao.get(currentUser);
}

@Override
public ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors)
        throws Exception {

    User user = (User) command;

    userDao.save(user);

    return new ModelAndView(getSuccessView());
}
}