如何使用ENUM列出角色?

huangapple go评论106阅读模式
英文:

How do I list roles using ENUM?

问题

以下是您要翻译的内容:

  1. // 用户控制器
  2. private UserRepository userRepository;
  3. public UserController(UserRepository userRepository) {
  4. this.userRepository = userRepository;
  5. }
  6. @PostMapping
  7. public String userPostAdd(@RequestBody User user) {
  8. List<Authority> rolesList = new ArrayList<>();
  9. for (Role role : user.getRole()){
  10. Authority r = role.getName();
  11. if (r != null){
  12. rolesList.add(r);
  13. }
  14. }
  15. if (!rolesList.isEmpty()){
  16. userRepository.save(user);
  17. }
  18. return "添加用户";
  19. }
  20. // 角色实体
  21. @ToString
  22. @Entity
  23. @Table(name = "role", schema = "task")
  24. public class Role {
  25. @Id
  26. @GeneratedValue(strategy = GenerationType.IDENTITY)
  27. private Long id;
  28. @Column(name = "name", nullable = false)
  29. @Enumerated(EnumType.STRING)
  30. private Authority name;
  31. @ManyToMany(fetch = FetchType.LAZY, mappedBy = "role")
  32. private List<User> users;
  33. // getter、setter...
  34. }
  35. // 用户实体
  36. @ToString
  37. @Entity
  38. @Table(name = "user", schema = "task")
  39. public class User {
  40. @Id
  41. @GeneratedValue(strategy= GenerationType.IDENTITY)
  42. @Column(name = "id")
  43. private Long id;
  44. @ManyToMany
  45. @JoinTable(name = "user_role",
  46. joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
  47. inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
  48. private List<Role> role;
  49. @OneToMany(mappedBy = "user")
  50. private Set<Contract> contract;
  51. @Column(name = "surname", nullable = false)
  52. private String surname;
  53. @Column(name = "name", nullable = false)
  54. private String name;
  55. @Column(name = "email", nullable = false, unique = true)
  56. private String email;
  57. @Column(name = "phone", nullable = false, unique = true)
  58. private Integer phone;
  59. // getter、setter...
  60. }
  61. // 枚举
  62. public enum Authority {
  63. TENANT ("tenant"),
  64. LANDLORD ("landlord"),
  65. ADMIN ("admin");
  66. private final String name;
  67. private Authority(String s) {
  68. name = s;
  69. }
  70. public boolean equalsName(String otherName) {
  71. return name.equals(otherName);
  72. }
  73. public String toString() {
  74. return this.name;
  75. }
  76. }
英文:

Example of a POST request that I will make to add a user: Vasya, Petrenko,910382741, vasya@mail.ru, ADMIN the first 4 attributes work, but I don't understand how to make a field for a role.

Is my database built correctly? I use Json to send a request to add a user, the user's relationship to the role is many to many. I don't understand how to send the role, I need another attribute in the user?

  1. UserController
  2. private UserRepository userRepository;
  3. public UserController(UserRepository userRepository) {
  4. this.userRepository = userRepository;
  5. }
  6. @PostMapping
  7. public String userPostAdd(@RequestBody User user) {
  8. List&lt;Authority&gt; rolesList = new ArrayList&lt;&gt;();
  9. for (Role role : user.getRole()){
  10. Authority r = role.getName();
  11. if (r != null){
  12. rolesList.add(r);
  13. }
  14. }
  15. if (!rolesList.isEmpty()){
  16. userRepository.save(user);
  17. }
  18. return &quot;Add user&quot;;
  19. }

Entity Role

  1. @ToString
  2. @Entity
  3. @Table(name = &quot;role&quot;, schema = &quot;task&quot;)
  4. public class Role {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. private Long id;
  8. @Column(name = &quot;name&quot;, nullable = false)
  9. @Enumerated(EnumType.STRING)
  10. private Authority name;
  11. @ManyToMany(fetch = FetchType.LAZY, mappedBy = &quot;role&quot;)
  12. private List&lt;User&gt; users;
  13. getter,setter...
  14. }

User:

  1. @ToString
  2. @Entity
  3. @Table(name = &quot;user&quot;, schema = &quot;task&quot;)
  4. public class User {
  5. @Id
  6. @GeneratedValue(strategy= GenerationType.IDENTITY)
  7. @Column(name = &quot;id&quot;)
  8. private Long id;
  9. @ManyToMany
  10. @JoinTable(name = &quot;user_role&quot;,
  11. joinColumns = @JoinColumn(name = &quot;role_id&quot;, referencedColumnName = &quot;id&quot;),
  12. inverseJoinColumns = @JoinColumn(name = &quot;user_id&quot;, referencedColumnName = &quot;id&quot;))
  13. private List&lt;Role&gt; role;
  14. @OneToMany(mappedBy = &quot;user&quot;)
  15. private Set&lt;Contract&gt; contract;
  16. @Column(name = &quot;surname&quot;, nullable = false)
  17. private String surname;
  18. @Column(name = &quot;name&quot;, nullable = false)
  19. private String name;
  20. @Column(name = &quot;email&quot;, nullable = false, unique = true)
  21. private String email;
  22. @Column(name = &quot;phone&quot;, nullable = false, unique = true)
  23. private Integer phone;
  24. getter,setter...
  25. }

Enum

  1. public enum Authority {
  2. TENANT (&quot;tenant&quot;),
  3. LANDLORD (&quot;landlord&quot;),
  4. ADMIN (&quot;admin&quot;);
  5. private final String name;
  6. private Authority(String s) {
  7. name = s;
  8. }
  9. public boolean equalsName(String otherName) {
  10. return name.equals(otherName);
  11. }
  12. public String toString() {
  13. return this.name;
  14. }
  15. }

如何使用ENUM列出角色?

答案1

得分: 1

问题在于你将 roles = null,然后对其进行方法调用。你首先需要通过调用 valueOf 方法来获取角色对象。如果角色不存在,它应该是 null。以下代码应该可以工作。

  1. @PostMapping
  2. public String userPostAdd(@ModelAttribute User user) {
  3. List<Roles> rolesList = new ArrayList<>();
  4. for (Role role : user.getRole()){
  5. Roles r = Roles.valueOf(role.getName().toUpperCase());
  6. if (r != null){
  7. rolesList.add(r);
  8. }
  9. }
  10. if (!rolesList.isEmpty()){
  11. userRepository.save(user);
  12. }
  13. return "Add user";
  14. }
英文:

The problem is that you're setting roles = null and then make a method call on it. You first need to fetch the role object by calling valueOf. If the role does not exist it should be null. The following should work.

  1. @PostMapping
  2. public String userPostAdd(@ModelAttribute User user) {
  3. List&lt;Roles&gt; rolesList = new ArrayList&lt;&gt;();
  4. for (Role role : user.getRole()){
  5. Roles r = Roles.valueOf(role.getName().toUpperCase());
  6. if (r != null){
  7. rolesList.add(r)
  8. }
  9. }
  10. if (!rolesList.isEmpty()){
  11. userRepository.save(user);
  12. }
  13. return &quot;Add user&quot;;
  14. }

huangapple
  • 本文由 发表于 2020年9月26日 02:28:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64069571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定