【发布时间】:2020-01-26 21:12:19
【问题描述】:
我正在使用 Spring 框架。我有两个实体,电影和演员,所以电影可以有很多演员,演员可以在很多电影中播放。以下是我们的课程:
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Actor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
private String age;
@ManyToMany
@JoinTable(name = "movie_actor")
private List<Movie> movies;
public Actor(String name, String surname, String age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<Movie> getMovies() {
return movies;
}
public void setMovies(List<Movie> movies) {
this.movies = movies;
}
}
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String genre;
private String year;
@ManyToMany(mappedBy = "movies")
private List<Actor> actors;
public Movie(String title, String genre, String year, List<Actor> actors) {
this.title = title;
this.genre = genre;
this.year = year;
this.actors = actors;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public List<Actor> getActors() {
return actors;
}
public void setActors(List<Actor> actors) {
this.actors = actors;
}
}
我使用@ManyToMany注解来定义它们之间的关系。
此时,我在 H2 中有一个具有 ID、AGE、NAME 和 SURNAME 列的表 Actor,具有 ID、GENRE、TITLE 和 YEAR 的表 Movie,以及一个新表 MOVIE_ACTOR,因为带有 ACTORS_ID 和 MOVIES_ID 的注释列。直到这里看起来还不错。
现在,如果我保存一部电影(我已经为两个实体实现了扩展 jpaRepository 的服务和存储库):
@GetMapping("/create")
public void create() {
Actor actor1 = new Actor("Pedro", "Perez", "40");
Actor actor2 = new Actor("Alfredo", "Mora", "25");
Actor actor3 = new Actor("Juan", "Martinez", "20");
Actor actor4 = new Actor("Mario", "Arenas", "30");
List<Actor> actorList = new ArrayList<>();
actorList.add(actor1);
actorList.add(actor2);
actorList.add(actor3);
actorList.add(actor4);
Movie movie = new Movie("Titanic", "Drama", "1984", actorList);
movieService.create(movie);
}
(我知道这不是获取请求,而只是为了检查是否正确保存了仅访问端点的电影)我得到的是在表 Movie 中正确添加了 4 列,但 ACTOR 和MOVIE_ACTOR 已完成,因此省略了 actorList,这两个表是空的。为什么会发生这种情况,我该如何解决?
非常感谢您的帮助!
【问题讨论】:
-
嗨,这不是建立多对多关系的最佳方式,请查看此帖子 vladmihalcea.com/… 以获得最佳和最有效的方式。
标签: java spring spring-boot spring-data-jpa