Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA merge() doesn't work as expected when resaving a modified object

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 new Actor 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.

like image 524
orange Avatar asked Jun 06 '20 01:06

orange


People also ask

How does merge work in JPA?

JPA's merge method copies the state of a detached entity to a managed instance of the same entity. Hibernate, therefore, executes an SQL SELECT statement to retrieve a managed entity from the database.

Does merge operation creates new copy of entity object?

Merge returns the managed instance that the state was merged with. It does return something that exists in PersistenceContext or creates a new instance of your entity. In any case, it will copy the state from the supplied entity, and return a managed copy.

Does EntityManager merge insert or update?

EntityManager. merge() can insert new objects and update existing ones.

What is the difference between persist and merge in JPA?

persist(Object entity): Make an instance managed and persistent. merge(T entity): Merge the state of the given entity into the current persistence context.


1 Answers

The EntityManager. merge() operation is used to merge the changes made to a detached object into the persistence context. merge does not directly update the object into the database, it merges the changes into the persistence context (transaction).

When the transaction is committed, or if the persistence context is flushed, then the object will be updated in the database.

For you merge is not required, although it is frequently misused. To update an object you simply need to read it, then change its state through its set methods, then commit the transaction. The EntityManager will figure out everything that has been changed and update the database. merge is only required when you have a detached copy of a persistence object.

Just in your case you arent doing commit of transaction.

entityManager.getTransaction().commit();
like image 67
Kunal Vohra Avatar answered Oct 14 '22 02:10

Kunal Vohra