0

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
    Can you show the `MovieOtherTitle`? Also when you create a new title, is the join column in the title table is correctly populated? – yamenk Nov 25 '17 at 16:13
  • https://pastebin.com/u8FquKY8 Yes. All fields in the column are correctly completed. –  Nov 25 '17 at 16:40

1 Answers1

0
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 
Amr Alaa
  • 545
  • 3
  • 7
  • I immediately need the ID of this saved object. The object movieOtherTitles will have it when added to the list? –  Nov 25 '17 at 19:58
  • Since you have the Cascade.All, then any call for the movie.save method will also save the new movieOtherTitles instance. did i get you question correctly?! – Amr Alaa Nov 25 '17 at 20:00
  • https://pastebin.com/AFDyhf5S This way I won't get an ID, which I need to add immediately to the list. –  Nov 25 '17 at 20:22
  • movie.getOtherTitles().get(0).getId – Amr Alaa Nov 25 '17 at 20:24
  • did you check it ? – Amr Alaa Nov 25 '17 at 20:45
  • I have a problem, because yesterday it worked, and today it is no longer working. https://zapodaj.net/ac2bf2eccf8bf.png.html –  Nov 26 '17 at 13:53
  • I have seen your another question and replied to it. you just forget to save the movie after adding the child list – Amr Alaa Nov 26 '17 at 13:59