Home CascadeType.Merge in entity relations with Spring Data
Reply: 1

CascadeType.Merge in entity relations with Spring Data

Igor Gorbunov
1#
Igor Gorbunov Published in 2017-12-07 08:52:54Z

I have two entities (Consultancy - the owner, and InterviewType) with @ManyToMany bidirectional relations. User can attach the interview type to the Consultancy. I know that in a case of we're using a Hibernate we can just invoke merge() method on EntityManager to persist a new transient entity or update a detach object, but what about save() method in Spring Data or Spring Data have another method? Does it make some effect on a depend entity of a @ManyToMany relations? In case of 'yes' in what way? Does it invoke a Hibernates merge() method under the hood?

Maciej Kowalski
2#
Maciej Kowalski Reply to 2017-12-07 09:01:41Z

If you look at the implementation from the 2.0.2 version:

@Transactional
public <S extends T> S save(S entity) {

    if (entityInformation.isNew(entity)) {
        em.persist(entity);
        return entity;
    } else {
        return em.merge(entity);
    }
}

The merge is invoked under the hood if the persistence provider decides that the passed entity new.. does not have an id / is not managed by the persistence provider.. So that 'new' depends a bit on the underlying implementation of the EntityInformation.

In general it should invoke the merge when there is a need though i had situations where I had to write my own custom merge logic as the implicit merge was not ivoked

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303517 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO