I have three Classes: Film, Actor and an intermediate class called FilmActor. So, the thing is that the class FilmActor has a ManyToOne relation with Film and Actor, and both of them have an OneToMany relation with the FilmActor class because I want to know all the movies of the actor and all the actors participating in the movie.
I tried using @JsonIgnore or @JsonManagedReference and @JsonBackReference but if I use them I loss the information of the actors when I search for a film and the information of the films when I search for an actor.
I would like to know if there is way so I can get all the information and avoid the infinite loops.
Film class:
@Entity
public class Film{
private List<FilmActor> cast;
@OneToMany(mappedBy="filmActorFilm")
public List<FilmActor> getCast() {
return cast;
}
}
Actor class:
@Entity
public class Actor{
private List<FilmActor> credits;
@OneToMany(mappedBy="filmActorActor")
public List<FilmActor> getCredits() {
return credits;
}
}
FilmActor class:
@Entity
public class FilmActor{
public Film filmActorFilm;
public Actor filmActorActor;
@ManyToOne(optional=false,fetch=FetchType.LAZY)
@JoinColumn(name="filmActorFilm")
public Film getFilmActorFilm() {
return filmActorFilm;
}
@ManyToOne(optional=false,fetch=FetchType.LAZY)
@JoinColumn(name="filmActorActor")
public Actor getFilmActorActor() {
return filmActorActor;
}
}
Edit
I think I wasn't clear enough at the begining. I'm trying to make a rest server with spring boot, my problem comes when I try to get the list of actors of a Film and the list of films of an Actor. When I try to get a Film the application gets the attribute Cast that is a List of FilmActor, and if i don't make anything, this causes an infinite loop because the class FilmActor also has the class Film.
I can avoid this easily using the annotation @JsonIgnore on the getter of the method getFilmActorActor or using both annotations @JsonManagedReference and @JsonBackReference in the correspondent methods of the classes Film and FilmActor. Whit these annotations I can avoid the infinite loop because when I get the film that the application skips the property with the @JsonIgnore in this case the filmActorFilm that is the film in question. The problem is that if I skip this property I won't get it when I search an Actor, and the only that I can know is the number of films that the actor participates in, but I also want to get the information of the film and for that I have to take off the Json annotattions. The same problem comes in the opposite way trying to get all the Actors that participated in a Film.
So what I'm looking for is a way to skip the property filmActorFilm only when I get a Film, and skip the property filmActorActor when I get an Actor.