GithubHelp home page GithubHelp logo

spring-data-jpa's Introduction

persist vs merge

@Transactional
@Override
public <S extends T> S save(S entity) {
    if (entityInformation.isNew(entity)) {
        em.persist(entity);
        return entity;
    } else {
        return em.merge(entity);
    }
}
  • 파라미터로 들어온 entity가 새로운 엔티티인지 확인하고, 새로운 엔티티라면 persist를 호출하고, 새로운 엔티티가 아니라면 merge를 호출한다.
  • persist는 엔티티를 영속화하는 메서드이다. 엔티티를 영속화한다는 것은 엔티티를 영속성 컨텍스트에 저장하는 것을 의미한다.
  • merge는 내부적으로 우선 select 쿼리를 날려서 존재하는 엔티티인지 확인한다.
    • 해당 엔티티가 존재하면 파라미터로 넘어온 엔티티의 값으로 업데이트하고 영속성 컨텍스트에 저장하고 반환한다. (이때 파라미터로 넘어온 엔티티의 값으로 업데이트 되는 것을 주의해야 한다.)
    • 해당 엔티티가 존재하지 않으면 persist 처럼 새로운 엔티티로 인식하고 영속성 컨텍스트에 저장하고 반환한다.
  • 엔티티 업데이트가 필요한 경우에는 트랜잭션 커밋 시점에 변경 감지(Dirty Checking)가 일어나도록 하자.
  • 결론적으로 merge는 사용하지 말자. persist를 사용하자.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.