javax.persistence.NonUniqueResultException: 查询没有 return 唯一结果: 3
javax.persistence.NonUniqueResultException: query did not return a unique result: 3
**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;
}
不幸的是,它没有将角色分配给用户,而是创建了一个异常类型:
查询没有 return 个唯一结果:3;嵌套异常是 javax.persistence.NonUniqueResultException:查询没有 return 唯一结果:3
然后我问是否有人可以帮助我
另一种方法像添加角色,添加用户它的狼牙棒它的问题
这可能是因为您可能有不止一个用户具有相同的 username
,因此您可以将 username
设置为唯一,或者对所有事务或持久性使用 user_id
。
**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;
}
不幸的是,它没有将角色分配给用户,而是创建了一个异常类型: 查询没有 return 个唯一结果:3;嵌套异常是 javax.persistence.NonUniqueResultException:查询没有 return 唯一结果:3 然后我问是否有人可以帮助我 另一种方法像添加角色,添加用户它的狼牙棒它的问题
这可能是因为您可能有不止一个用户具有相同的 username
,因此您可以将 username
设置为唯一,或者对所有事务或持久性使用 user_id
。