如何解决JPA 存储库 Findbyid 返回重复列表
我的 retriveUser 方法有问题。当我运行 finbyid 方法时,它返回相同的重复值。您可以在下面看到我的存储库、用户和 userService 类以及结果。
[![findbyid 方法结果][1]][1]
我的用户存储库类
package io.javabrains.springsecurity.jpa;
import io.javabrains.*;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import io.javabrains.springsecurity.jpa.models.User;
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
Optional<User> findByUserName(String userName);
}
我的用户类
@Entity
@Table(name="app_user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy =GenerationType.IDENTITY)
private int id;
private String userName;
private String password;
private boolean active;
private String role;
private String city;
public User(String userName,boolean active,String role,String city) {
super();
this.userName = userName;
this.active = active;
this.role = role;
this.city = city;
}
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST)
@JoinTable(name = "user_cities",joinColumns = @JoinColumn(name = "user_id",referencedColumnName = "id"),inverseJoinColumns = @JoinColumn(name = "city_id",referencedColumnName = "id"))
private Collection<UserCity> usercities = new ArrayList<UserCity>() ;
public Collection<UserCity> getUsercities() {
return usercities;
}
public void setUsercities(Collection<UserCity> usercities) {
this.usercities = usercities;
}
public User() {}
public int getId() {
return id;
}
public void setId(int 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 isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
我的服务类
@RestController
public class HomeResource {
@Autowired
private BCryptPasswordEncoder bcryptPasswordEncoder;
@Autowired
private WeatherService weatherService;
@Autowired
private CityRepository cityRepository;
@Autowired
private UserRepository userRepo;
@GetMapping("/")
public String home() {
return ("<h1>Welcome</h1>");
}
@GetMapping("/user")
public String user() {
return ("Welcome User");
}
@GetMapping("/admin")
public String admin() {
return ("<h1>Welcome Admin</h1>");
}
@GetMapping("/getCities")
public List<UserCity> getCities()
{
return cityRepository.findAll();
}
@GetMapping("/users/{id}")
public ResponseEntity<User> retriveUser(@PathVariable int id){
Optional<User> a=userRepo.findById(id);
return new ResponseEntity<User>(a.get(),HttpStatus.OK);
}
预先感谢您的帮助。
真诚的 [1]:https://i.stack.imgur.com/gNhO7.png
解决方法
重复值是 usercities
对象中 User
集合中的嵌套用户。
用户 (id: 1
) 有一个 usercities
集合,其中包含一个 UserCity 对象(cityName: 'bursa'
和 users
包含同一用户 (id: 1
))。因此,用户 (id: 1
) 被递归显示。
您可以将@JsonIgnore 注释添加到您的属性(用户中的usercities
或用户城市中的users
)以减少递归。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。