I have two classes Webtoon :
@Entity
@Data
@NoArgsConstructor
public class Webtoon {
@Id //The unique id of the webtoon.
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="webtoon", orphanRemoval = true)
@CollectionTable(name= "list_of_episodes")
List<Episode> listOfEpisodes = new ArrayList<>();
@Column(name= "price", unique = false, nullable = false)
private BigDecimal price;
private String repositoryGeneratedId;
/**
* Add episode in the list
* @param episode
*/
public void addEpisode(Episode episode) {
listOfEpisodes.add(episode);
episode.setWebtoon(this);
}
/**
* Remove episode in the list
* @param episode
*/
public void removeEpisode(Episode episode) {
listOfEpisodes.remove(episode);
episode.setWebtoon(null);
}
/**
* Root url where all files are hosted in nexus
* @return
*/
@JsonIgnore
public String getRootUrl() {
return this.companyId+"/webtoon/webtoon-"+this.repositoryGeneratedId;
}
}
and Episode
@Entity
@Data
@NoArgsConstructor
public class Episode {
@Id //The unique id.
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name= "title", unique = false, nullable = false)
private String title;
@Column(name= "description", unique = false, nullable = false)
private String description;
@Column(name= "price", unique = false, nullable = false)
private BigDecimal price;
@OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
private Image icon;
@OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
private Image episodeNexus;
private String repositoryGeneratedId;
@JsonIgnore
@ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name="webtoon_id")
Webtoon webtoon;
}
This is my test :
- I created a webtoon
- I created an episode
- Add the episode in the episodeList using addEpisode
- Created the object episodeNexus and save all.
In my episodeList, I have the episode created but episodeNexus is Null. Why the list is not automatically updated ?
When I fetch once again the webtoon by Id, I have the correct list with the item created inside the episode.
How could after updated Episode to have my webtoon updated as well ?