0

I have a entity mapping like this: enter image description here

As you can see it is the bidirectional relationship and the team_users table has it own primary key and extra column - active.

Key code in team entity:

@OneToMany(mappedBy = "team", fetch = FetchType.LAZY)
    private Set<TeamUsers> team_users = new HashSet<TeamUsers>();

Key code in user entity:

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    private Set<TeamUsers> team_users = new HashSet<TeamUsers>();

Key code in team_user entity:

@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
    @JoinColumn(name = "TEAM_ID")
    private Team team;
    @ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
    @JoinColumn(name = "USER_ID")
    private User user;

I have a API which will return all team information, then I have service class like:

    @Autowired
    private TeamRepository teamRepo;

    public List<Team> listAll() {
        return (List<Team>) teamRepo.findAll(); 
    } 

Then the chrome log me that I have a "undefined" value, and when I test it by postman it shows me the infinite loop value: enter image description here

I want to figure out what is the best approach to fetch the data? I want to all information that tells me the team situation, including team... users...active status , almost everthing.

Any suggestions?

update

I tried to use @JsonIgnore on intermidate table :

@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "TEAM_ID")
private Team team;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "USER_ID")
private User user;

but I won't get user information in that case and it avoid infinite loop:

enter image description here

What else I can do to get all information for teams?

PEIRAN LIU
  • 157
  • 2
  • 11

0 Answers0