I have a Entity Movie and then Person Entity. Movie has a ManyToMany relationship with Person: one by casting, another by production and another by directio.
So my question is given a Person how I can obtain the movies he has acted, produced and directed.
//Person class:
@Entity
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@JsonView(Views.Private.class)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPerson;
private String name;
private String surnames;
private boolean isActor;
private boolean isDirector;
private boolean isProducer;
@ManyToMany(mappedBy = "casting")
private Set<Movie> moviesCasting = new HashSet<>();
@ManyToMany(mappedBy = "movieDirection")
private Set<Movie> movieDirection = new HashSet<>();
@ManyToMany(mappedBy = "movieProduction")
private Set<Movie> movieProduction = new HashSet<>();
//Movie class
@Entity
public class Movie implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "casting_movie",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "idPerson")
)
private Set<Person> casting = new HashSet<>();
public Set<Person> getCasting() {
casting.size();
return casting;
}
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "production_movie",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "idPerson")
)
private Set<Person> movieProduction = new HashSet<>();
public Set<Person> getMovieProduction() {
movieProduction.size();
return movieProduction;
}
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "direction_movie",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "idPerson")
)
private Set<Person> movieDirection = new HashSet<>();
getters and setters(...)
When I want to obtain information about a Person I get the error: failed to lazily initialize a collection of role Movies.casting”
If I given a Person I want to obtain each "filmography" I have to create the ManyToMany Relationship bidirectional?
You can have another entity which will have many-to-one references to Movie and Person with one additional field RelationshipType (with values Casting, Production, Direction etc). You should create a enum for that.
By doing so, you can fetch a person's directed movies like (from new entity): findByMovieAndPersonAndRelationshipType(Movie movie, Person person, RelationshipType.DIRECTED);
PS get rid of your all many-to-many relationships
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.