简体   繁体   中英

How do I list roles using ENUM?

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?

UserController

private UserRepository userRepository;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }


@PostMapping
public String userPostAdd(@RequestBody User user) {
    List<Authority> rolesList = new ArrayList<>();
    for (Role role : user.getRole()){
        Authority r = role.getName();
        if (r != null){
            rolesList.add(r);
        }
    }

    if (!rolesList.isEmpty()){
        userRepository.save(user);
    }
    return "Add user";
}

Entity Role

@ToString
@Entity
@Table(name = "role", schema = "task")
public class Role {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name", nullable = false)
@Enumerated(EnumType.STRING)
private Authority name;

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "role")
private List<User> users;

getter,setter...

}

User:

@ToString
@Entity
@Table(name = "user", schema = "task")
public class User {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@ManyToMany
@JoinTable(name = "user_role",
        joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private List<Role> role;

@OneToMany(mappedBy = "user")
private Set<Contract> contract;

@Column(name = "surname", nullable = false)
private String surname;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "phone", nullable = false, unique = true)
private Integer phone;

getter,setter...

}

Enum

public enum Authority {
    TENANT ("tenant"),
    LANDLORD ("landlord"),
    ADMIN ("admin");

    private final String name;

    private Authority(String s) {
        name = s;
    }

    public boolean equalsName(String otherName) {
        return name.equals(otherName);
    }

    public String toString() {
        return this.name;
    }
}

在此处输入图片说明

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.

@PostMapping
public String userPostAdd(@ModelAttribute User user) {
    List<Roles> rolesList = new ArrayList<>();
    for (Role role : user.getRole()){
        Roles r = Roles.valueOf(role.getName().toUpperCase());
        if (r != null){
            rolesList.add(r)
        } 
    }
    
    if (!rolesList.isEmpty()){
       userRepository.save(user);
    } 
    return "Add user";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM