public User editUser(User user) {
   List<Long> groupIds = new ArrayList<Long>();
   if (user.getGroupIds() != null) {
     groupIds = user.getGroupIds();
   }
   getJdbcTemplate()
       .update(
           "update users set username = ?,description=? where id = ?",
           new Object[] {user.getUserName(), user.getDescription(), user.getId()});
   //        return getGroupById(group.getId());
   deleteUserGroupRelations(user.getId());
   for (Long groupId : groupIds) {
     insertGroupUserRelation(user.getId(), groupId);
   }
   return user;
 }
  public User addUser(User user) {

    final String INSERT_SQL =
        "INSERT INTO users(username,password,first_name,last_name,description)"
            + " values(?,?,?,?,?)";
    final String username = user.getUserName();

    final String password = user.getPassword();

    final String firstName = user.getFirst_name();

    final String lastName = user.getLast_name();
    final String description = user.getDescription();

    List<Long> groupIds = new ArrayList<Long>();
    if (user.getGroupIds() != null) {
      groupIds = user.getGroupIds();
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getJdbcTemplate()
        .update(
            new PreparedStatementCreator() {

              public PreparedStatement createPreparedStatement(Connection connection)
                  throws SQLException {
                PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] {"id"});
                ps.setString(1, username);
                ps.setString(2, password);
                ps.setString(3, firstName);
                ps.setString(4, lastName);
                ps.setString(5, description);
                return ps;
              }
            },
            keyHolder);

    user.setId(keyHolder.getKey().longValue());
    // keyHolder.getKey() now contains the generated key
    for (Long groupId : groupIds) {
      insertGroupUserRelation(user.getId(), groupId);
    }
    return user;
  }