#StackBounty: #java #jpa #persistence #javax.persistence JPA merge() doesn't work as expected when resaving a modified object

Bounty: 500

This is my set up for the entity

@Entity
@Getter
@Setter
@Table(name = "movies", schema = "public")
public class Movie {
    @Id
    @Column(name = "id")
    private UUID movieId;

    @OneToMany(mappedBy = "actor", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Actors> actors = new HashSet<>();

    //Getters and setters
    }
}
@Entity
@Getter
@Setter
@Table(name = "actors_movies", schema = "public")
public class ActorMovie {

    @EmbeddedId
    private ActorId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("movieId")
    @JoinColumn(name = "id_movie_movie")
    private Movie movie;

    //Getters and setters below
}

I am able to successfully call entityManager.merge()' when I add a newActor` to the list of movie. The following successfully persists the tables.

Movie movie = getMovieById(id);
movie.setActors(new Actor());
entityManager.merge(movie);

Then I want to update the actor name so what I do is this. I am NOT able to update an already persisted/existing Actor:

Movie movie = getMovieById(id);
movie.getActors().get(0).setName("New Name");
entityManager.merge(movie);

The above doesn’t work, I can see the object being updated in debug mode but when merge() is called it doesn’t update the database.

Where am I going wrong? I thought merge would update if already existing.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.