title: javax.persistence.NonUniqueResultException: query did not return a unique result: 3
body: <pre><code>**hi big community! someone can help me**
   **here: Role entity**
  
          @Entity
          @Table(name="roles")
          public class Role implements Serializable {
          @Id @GeneratedValue(strategy = GenerationType.AUTO)
          private Integer id;  
          private String obser;
          public Role() {
              super();
          }
  
      public Role(Integer id, String obser) {
          super();
          this.id = id;
          this.obser = obser;
      }
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getObser() {
          return obser;
      }
  
      public void setObser(String obser) {
          this.obser = obser;
      }}
  
  
  
      @Entity
      @Table(name="users")
      public class User implements Serializable{
  
      @Id @GeneratedValue(strategy=GenerationType.AUTO)
      private Integer id;
      private String username;
      private String password;
      private Boolean active;
      private String email;
  
  
        @ManyToMany
        @JoinTable(name="USERS_ROLES", 
                joinColumns={@JoinColumn(name="username", referencedColumnName="username")},
                 inverseJoinColumns={@JoinColumn(name="obser", referencedColumnName="obser")}
  
                )
      private Collection&lt;Role&gt;roles;
      public Collection&lt;Role&gt; getRoles() {
          return roles;
      }
      public void setRoles(Collection&lt;Role&gt; roles) {
          this.roles = roles;
      }
      public User() {
          super();
      }
      public User(Integer id,String username, String password, Boolean active, String email) {
          super();
          this.id = id;
          this.username = username;
          this.password = password;
          this.active = active;
          this.email = email;
      }
      public Integer getId() {
          return id;
      }
      public void setId(Integer id) {
          this.id = id;
      }
      public String getUsername() {
          return username;
      }
      public void setUsername(String username) {
          this.username = username;
      }
      public String getPassword() {
          return password;
      }
      public void setPassword(String password) {
          this.password = password;
      }
      public Boolean getActive() {
          return active;
      }
      public void setActive(Boolean active) {
          this.active = active;
      }
      public String getEmail() {
          return email;
      }
      public void setEmail(String email) {
          this.email = email;
      }}
  
  **here is y Role RoleRepository:**
  
  
      public interface RoleRepository extends JpaRepository&lt;Role, Integer&gt; {
          List&lt;Role&gt; findAll();
          Role findByObser(String obser);
      }
  
  **here is my User RoleRepository:**
  
      public interface UserRepository extends JpaRepository&lt;User, Integer&gt; {
          User findByUsername(String username);
      }
  
  
  **here is my function AddRoleToUser in  my UserService:**
  **When I want to add a role to a user, I use this method**
  
      @RequestMapping(value="/addRoleToUser")
      public User addRoleToUser(String username, String role) {
          User u = userRepository.findByUsername(username);
          Role r = roleRepository.findByObser(role);
  
  
  
        u.getRoles().add(r); 
                userRepository.save(u);
              return u;
      }
  </code></pre>
  
  <p><strong>Unfortunately, it does not assign the role to the user and it creates an exception of type:
  query did not return a unique result: 3; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 3 
  then I ask if there is someone who can help me
  the other method like add role, add user its mace its problem</strong></p>
  
  <p><a href="https://i.stack.imgur.com/k8snD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k8snD.jpg" alt="enter image description here"></a></p>