Reputation: 39
**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<Role>roles;
public Collection<Role> getRoles() {
return roles;
}
public void setRoles(Collection<Role> 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<Role, Integer> {
List<Role> findAll();
Role findByObser(String obser);
}
**here is my User RoleRepository:**
public interface UserRepository extends JpaRepository<User, Integer> {
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;
}
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
Upvotes: 0
Views: 12279
Reputation: 78
This might be because you might have more then one user with same username
, so either you can set username
as unique or use user_id
for all the transactions or persistence.
Upvotes: 1