[英]Multiple ManyToMany using JPA Hibernate
我想在電影和人物之間創建一個ManyToMany關系。 我特別需要三個關系。
問題在於它創建帶有該列的SINGLE表:
movieProduction_id | movieProduction_idPerson | movieDirection_id | movieDirection_idPerson | moviesCasting_id | casting_idPerson
我需要創建三個不同的表,一個用於movieProduction,一個用於movieDirection,另一個用於movieCasting。
當我想通過ID獲取電影時,我也收到錯誤消息““無法延遲初始化角色集合:org.udg.pds.simpleapp_javaee.model.Movie.casting,無法初始化代理-沒有會話”。嘗試使用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<>();
對於每個關系創建表,您應該使用
@ManyToMany @JoinTable(name = "foo") public List<Person> getCasting(){ return casting;
上面的代碼將具有3個聯結表。
另外,由於您的懶惰問題,您有2種選擇取決於您的問題。 您可以為所有3個聯結設置fetch = FetchType.EAGER)
。 或者嘗試在事務關閉之前獲取關系數據。 (它需要查看您的事務配置和dao)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.