多个ManyToMany使用JPA Hibernate

时间:2018-05-12 15:24:39

标签: java hibernate jpa fetch

我想在Movie和Person之间创建一个ManyToMany关系。 特别是我需要三种关系。

问题是它创建了一个包含这些列的SINGLE表:

movieProduction_id | movieProduction_idPerson | movieDirection_id | movieDirection_idPerson | moviesCasting_id | casting_idPerson

我需要创建三个不同的表,一个用于movieProduction,一个用于movieDirection,另一个用于movieCasting。

此外,我收到错误""未能懒惰地初始化角色集合:org.udg.pds.simpleapp_javaee.model.Movie.casting,无法初始化代理 - 无会话"当我想通过ID获取电影时。我也尝试使用fetchType = EAGER,但是intellij没有编译。

电影:

@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;
}

人:

@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<>();

0 个答案:

没有答案