[Spring][Rest][JPA] Problem z jsonem

0

Witam, mam problem z obiektami w JSONie.

Diagram mojej bazy:
https://image.prntscr.com/image/8d5MKhIZSHKKRu2uUtsu4g.png

@Entity
@XmlRootElement
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;
    private String username;
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private Timestamp registrationDate;

    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @JoinTable(name = "userUserRole",
            joinColumns = {@JoinColumn(name = "idUser", referencedColumnName = "userId")},
            inverseJoinColumns = {@JoinColumn(name = "idUserRole", referencedColumnName = "userRoleId")})
    private Set<UserRole> userRoles = new HashSet<>();

    @OneToMany(targetEntity = Discovery.class,
            mappedBy = "user", fetch = FetchType.EAGER,
            cascade = CascadeType.ALL)
    private List<Discovery> discoveries = new ArrayList<>();

    @OneToMany(targetEntity = Vote.class, mappedBy = "user", cascade = CascadeType.ALL)
    private List<Vote> votes = new ArrayList<>();


@Entity
public class Discovery  {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long discoveryId;
    private String name;
    @Column(length = 1024)
    private String description;
    private String url;
    private Timestamp date;
    private int upVote;
    private int downVote;

    @ManyToOne(targetEntity = User.class)
    private User user;

    @OneToMany(mappedBy = "discovery", targetEntity = Vote.class,
            fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Vote> votes = new ArrayList<>();


@RestController
@RequestMapping("api/users")
public class UserEndpoint {

    private UserRepository userRepository;

    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public List<User> getAll() {
        return userRepository.findAll();
    }

UserRepository to zwykły interfejs który rozszerza JpaRepository<T, K>

Gdy przypisuje do jakiegoś User obiekt typu Discovery JSON sie "psuje"
https://image.prntscr.com/image/TEkrrid6TSmfZ2XNgumMGQ.png

Wszystko działa gdy używam Spring Boot Data, ale chce sam stworzyć endpointy, proszę o pomoc bo walczę z tym cały dzień.

0

Nie rozumiem. Nic się nie "psuje". Działa dokładnie tak ja to zaklepałeś w kodzie.Zrobiłeś sobie powiązanie dwustronne to potem takiego jsona ci to produkuje.

  1. Nie baw sie obiektami entity. Pobierasz z bazy a następnie transformujesz na jakieś pojo, bo inaczej prosisz się o problemy.
  2. Jeśli masz gdzieś takie pola których nie chcesz serializować to możesz dać @XmlTransient i wtedy JAXB nie będzie tych pól brać pod uwagę.
0
Shalom napisał(a):
  1. Jeśli masz gdzieś takie pola których nie chcesz serializować to możesz dać @XmlTransient i wtedy JAXB nie będzie tych pól brać pod uwagę.

Albo @JsonIgnore

0

Hej,

Czy na pewno potrzebujesz fetch type EAGER ? Dociągając zależności osobno unikniesz wielu problemów.

1 użytkowników online, w tym zalogowanych: 0, gości: 1