简体   繁体   中英

JPA Repository Findbyid Returns Repeated List

I am having a problem with my retriveUser method. When I run finbyid method it returns the same repeated values. You can see my repository, user, and userService classes and the result below.

[![findbyid method result][1]][1]

My User Repository Class

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);

}

My User Class

@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;
    }
    

}

My Service Class

@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);
   
} 

Thanks in advance for your help.

Sincerely [1]: https://i.stack.imgur.com/gNhO7.png

The repeated value is a nested user from the usercities collection in a User object.

The user ( id: 1 ) has a usercities collection containing one UserCity object ( cityName: 'bursa' and users containing the same user ( id: 1 )). Thus, the user ( id: 1 ) is recursively displayed.

You can add @JsonIgnore annotation to your property ( usercities in User or users in UserCity) to cut the recursion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM