1

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 :

  1. I created a webtoon
  2. I created an episode
  3. Add the episode in the episodeList using addEpisode
  4. 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 ?

Teddy Kossoko
  • 1,168
  • 3
  • 20
  • 48
  • Does this answer your question? [Hibernate bidirectional @ManyToOne, updating the not owning side not working](https://stackoverflow.com/questions/5460573/hibernate-bidirectional-manytoone-updating-the-not-owning-side-not-working) – Jens Schauder May 25 '20 at 09:20

1 Answers1

0

use unidirectional mapping in your class bidirectional mapping sometimes give problems. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "foreign-key-name")List<Episode> listOfEpisodes = new ArrayList<>(); and remove @ManyToOne annotation from the Episode table

Paradox
  • 19
  • 4
  • with a mono-relation I have and error when I delete an episode, because i don't have the cascade delete for a table of associations between Episode and Webtoon – Teddy Kossoko Jan 22 '20 at 10:09
  • first of tell me the exact problem that comes on the mono-relationship....here you can also use @ManyToMany ``@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinTable(name = "episodes_webtom", joinColumns = @JoinColumn(name = "episode_id"), inverseJoinColumns = @JoinColumn(name = "webtom_id")) private List episodes = new ArrayList(); `` – Paradox Jan 24 '20 at 13:03