程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了更新多对多表用户中的角色大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决更新多对多表用户中的角色?

开发过程中遇到更新多对多表用户中的角色的问题如何解决?下面主要结合日常开发的经验,给出你关于更新多对多表用户中的角色的解决方法建议,希望对你解决更新多对多表用户中的角色有所启发或帮助;

我正在按照教程@R_267_10589@注册并使用 spring 登录。这是我的结构项目:

@L_301_0@

我的 User.java

@Entity
@table(uniqueConsTraints = @UniqueConsTraint(columnnames = "email"))
public class User {
@ID
@GeneratedValue(strategy = GenerationType.auto)
private Long ID;

private String firstname;
private String lastname;
private String email;
private String password;

@manyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@Jointable(
        name = "users_roles",joincolumns = @Joincolumn(
                name = "user_iD",referencedcolumnname = "ID"),inverseJoincolumns = @Joincolumn(
                name = "role_ID",referencedcolumnname = "ID"))
private Collection<Role> roles;

public User() {

}

public User(String firstname,String lastname,String email,String password) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;
    this.password = password;
}

public User(String firstname,String password,Collection<Role> roles) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;
    this.password = password;
    this.roles = roles;
}
 //getter & setter

我的角色.java

@Entity
public class Role {
@ID
@GeneratedValue(strategy = GenerationType.auto)
private Long ID;
private String name;

public Role() {
}

public Role(String Name) {
    this.name = name;
} //getter & setter

我试图在我的 UserRepository.java 上查询,但它不起作用。

@modifying
@query(value = "update User u set u.roles = ?1 where u.id = ?1")
voID updateRole(Collection<Role> roles,Long user_iD);

我的 MainController.java

@requestMapPing(value = "/user/update-role")
public String updateRole(@ValID @modelattribute("data") User user,@Param("ID") Long IDUser,@Param("role") String role,BindingResult result) {
    String email = user.getEmail();

    Role roleUser = roleRepository.findByname(rolE);
    Collection<Role> roleupdate = Arrays.asList(roleUser);
    System.out.println(roleupdatE);
    System.out.println(user);
    try {
        if (result.hasErrors()) {
            return "user/List";
        }
        userRepository.updateRole(roleupdate,IDUser);
    } catch (Exception E) {
        logger.error("Error data: " + E);
    }
    String url = "redirect:/user/detail/"+ email + "?success=true";

    return url;
}

我收到错误:解析模板时出错 [error-500],模板可能不存在或可能无法被任何已配置的模板解析器访问。 如何只更新 User 中的角色。你有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的更新多对多表用户中的角色全部内容,希望文章能够帮你解决更新多对多表用户中的角色所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: