这是我为实体设置的:
@Entity
@Getter
@Setter
@Table(name = "movies", schema = "public")
public class Movie {
@Id
@Column(name = "id")
private UUID movieId;
@OneToMany(mappedBy = "actor", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Actors> actors = new HashSet<>();
//Getters and setters
}
}
@Entity
@Getter
@Setter
@Table(name = "actors_movies", schema = "public")
public class ActorMovie {
@EmbeddedId
private ActorId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("movieId")
@JoinColumn(name = "id_movie_movie")
private Movie movie;
//Getters and setters below
}
entityManager.merge()
当我将新电影添加Actor
到电影列表时,我能够成功调用。以下成功持久化了表。
Movie movie = getMovieById(id);
movie.setActors(new Actor());
entityManager.merge(movie);
然后我想更新演员的名字,所以我要做的就是这个。我无法更新已经持久化/现有的 Actor:
Movie movie = getMovieById(id);
movie.getActors().get(0).setName("New Name");
entityManager.merge(movie);
以上不起作用,我可以看到在调试模式下正在更新的对象但是当merge()
被调用时它不会更新数据库。
我哪里错了?我认为如果已经存在,合并会更新。