I want to create a ManyToMany relation between Movie and Person. Specially I need three relationships.
The problem is that it creates a SINGLE table with that columns:
movieProduction_id | movieProduction_idPerson | movieDirection_id | movieDirection_idPerson | moviesCasting_id | casting_idPerson
I need to create three different tables, one for movieProduction, one for movieDirection and another for movieCasting.
Also I get the error ""failed to lazily initialize a collection of role: org.udg.pds.simpleapp_javaee.model.Movie.casting, could not initialize proxy - no Session" when I want to GET a movie by ID. I also tried using fetchType = EAGER, but then intellij doesn't compile.
Movie:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch = FetchType.EAGER)
private List<Genre> genres = new ArrayList<>();
public List<Genre> getGenres() {
genres.size();
return genres;
}
@ManyToMany(cascade = {CascadeType.ALL})
private List<Person> casting = new ArrayList<>();
public List<Person> getCasting() {
casting.size();
return casting;
}
@ManyToMany(cascade = {CascadeType.ALL})
private List<Person> movieProduction = new ArrayList<>();
public List<Person> getMovieProduction() {
movieProduction.size();
return movieProduction;
}
@ManyToMany(cascade = {CascadeType.ALL})
private List<Person> movieDirection = new ArrayList<>();
public List<Person> getmovieDirection() {
movieDirection.size();
return movieDirection;
}
Person:
@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 String nacionality;
private String date_birth;
private boolean isActor;
private boolean isDirector;
private boolean isProducer;
@ManyToMany(mappedBy = "casting")
private List<Movie> moviesCasting = new ArrayList<>();
@ManyToMany(mappedBy = "movieDirection")
private List<Movie> movieDirection = new ArrayList<>();
@ManyToMany(mappedBy = "movieProduction")
private List<Movie> movieProduction = new ArrayList<>();
For creating table per relation you should use
@ManyToMany @JoinTable(name = "foo") public List<Person> getCasting(){ return casting;
With code above you will have 3 junction table.
Also with your lazy problem you have 2 choice depended on you Dao thing. You can set fetch = FetchType.EAGER)
for all 3 junctions. Or trying to fetch relation datas before the transaction is closed. (It needs to see your transaction configuration and dao)
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.