Entity update in EntityManager

我是研究僧i 提交于 2020-01-25 10:48:05

问题


Describe briefly the whole situation. I have an entity

public class MovieEntity {
... 
    @OneToMany(mappedBy = "movie", cascade = CascadeType.ALL)
    private Set<MovieOtherTitle> otherTitles;
...
}

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.


回答1:


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

So after calling movie.getOtherTitles(); the list will not be empty

you can get the id of the otherTitle

movie.getOtherTitles().get(0).getId 


来源:https://stackoverflow.com/questions/47487723/entity-update-in-entitymanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!