EntityManager中的实体更新 [英] Entity update in EntityManager
问题描述
简要描述整个情况。我有一个实体
Describe briefly the whole situation. I have an entity
public class MovieEntity {
...
@OneToMany(mappedBy = "movie", cascade = CascadeType.ALL)
private Set<MovieOtherTitle> otherTitles;
...
}
我使用JPA Spring数据存储库
I save a movie in the database using the JPA Spring Data repository
this.movieRepository.save(movie);
然后我在影片中添加另一个标题
Then I add another title to the film
final MovieOtherTitle movieOtherTitle = new MovieOtherTitle(otherTitle.getTitle(), otherTitle.getCountry());
movieOtherTitle.setMovie(movie);
movieOtherTitle.setUser(user);
this.entityManager.persist(element);
当我想显示这部电影的标题列表时,该列表为空白
And when I want to display a list of titles for this movie, the list is BLANK
movie.getOtherTitles()
重新编译应用程序后,一切正常。为什么?似乎该实体没有实时刷新列表。我必须再次编译该应用程序,然后才能看到列表中的元素。
After recompile the application, everything is OK. WHY? It looks as if this entity did not refresh the list in real time. I have to compile the application again and only then you can see the elements in the list.
推荐答案
this.movieRepository.save(movie);
final MovieOtherTitle movieOtherTitle = new MovieOtherTitle(otherTitle.getTitle(), otherTitle.getCountry());
movieOtherTitle.setMovie(movie);
movieOtherTitle.setUser(user);
movie.getOtherTitles().add(movieOtherTitles);
this.movieReoository.save(movie);
因此,在调用
movie.getOtherTitles()之后,
列表不会为空
So after calling movie.getOtherTitles(); the list will not be empty
您可以获取另一个标题的ID
you can get the id of the otherTitle
movie.getOtherTitles().get(0).getId
这篇关于EntityManager中的实体更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!