title: How to set Role when I am creating new User?
body: <p>I've got simple application in Spring and I want to implement method which set role when i am creating new user. So Its my code. How should I give Set roles to last method?</p>
  
  <p>Role.java </p>
  
  <pre><code>@Entity
  @Table(name = "roles")
  public class Role {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Enumerated(EnumType.STRING)
      @NaturalId
      private RoleName name;
  
      public Role(){}
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public RoleName getName() {
          return name;
      }
  
      public void setName(RoleName name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>RoleName enum</p>
  
  <pre><code>    public enum RoleName {
      ROLE_USER,
      ROLE_ADMIN
  }
  </code></pre>
  
  <p>Setter method in User.java</p>
  
  <pre><code>@ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(name = "user_roles",
          joinColumns = @JoinColumn(name = "user_id"),
          inverseJoinColumns = @JoinColumn(name = "role_id"))
  private Set&lt;Role&gt; roles = new HashSet&lt;&gt;();
  </code></pre>
  
  <p>And how should I fill this method?</p>
  
  <pre><code>  public User addUser(User user) {
      Role userRole = roleRepository.findRoleByName(RoleName.ROLE_USER);
      User newUser = new User();
      newUser.setEmail(user.getEmail());
      newUser.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
      newUser.setUsername(user.getUsername());
      newUser.setFirstName(user.getFirstName());
      newUser.setLastName(user.getLastName());
      newUser.setRoles();
      return userRepository.save(newUser);
  }
  </code></pre>
  
  <p>I don't know what should i put in <code>newUser.setRoles()</code></p>