0

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.

crg5512
  • 21
  • 1
  • 6
  • you are showing jpa/hibernate annotations. what's the relation to jackson? – Sharon Ben Asher Aug 09 '18 at 05:30
  • @SharonBenAsher the relation with Jackson is that springboot uses it to return the fields of the classes in JSON, I think. Also the properties that I mention (JsonIgnore, JsonManagedReference and JsonBackReference) belong to Jackson. Btw I think I wasn't really clear with my question so I'm going to edit it. – crg5512 Aug 09 '18 at 12:55

2 Answers2

0

You can probably have a look at both the answers (which had been answered earlier at StackOverflow) for the respective questions viz. Question1 & Question2. You will definitely get some deep insight into how to use these annotations (@OneToMany, @ManyToOne, @JoinColumn etc.) in hibernate efficiently so that you can get optimum result.

Abhinav
  • 530
  • 8
  • 21
  • Sorry I wasn't really clear with my question and this isn't what I'm looking for, I'm going to edit my question. – crg5512 Aug 09 '18 at 12:58
0

I had the same problem as you. For my case is that I have sourceBatch and childBatch instead of Actor and Film and have to @JsonIgnore my childBatch.

Here's my workaround,

-- Create a transient Field for the value that is ignored in your entity class (also its getter and setter),

@Transient
@JsonProperty('tempChildBatches')
private Collection<Batch> tempChildBatches = new ArrayList<>();

-- In @Controller class method that passes the data to front-end,

Collection<Batch> tempHolder = batch.getChildBatches(); //Original JsonIgnored field getter method
batch.setTempChildBatches(tempHolder); //Setter for the transient field

Now your front-end should be able to receive the @JsonIgnore data through this transient field. Hope it helps.

IzL
  • 1
  • 2