jpa存储库findbyid返回重复列表

m3eecexj  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(455)

我的retrieveuser方法有问题。当我运行finbyid方法时,它返回相同的重复值。您可以看到我的存储库、用户和用户服务类以及下面的结果。
[![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

sycxhyv7

sycxhyv71#

重复值是来自 usercities 收藏于 User 对象。
用户( id: 1 )有一个 usercities 包含一个usercity对象的集合( cityName: 'bursa' 以及 users 包含相同的用户( id: 1 )). 因此,用户( id: 1 )递归显示。
可以将@jsonignore注解添加到属性中( usercities 在用户或 users 在usercity中)来剪切递归。

相关问题