How spring data JPA decides to call entityManager.persist(…) or entityManager.merge(…) method

前端 未结 1 509
旧时难觅i
旧时难觅i 2020-12-31 14:07

When entityManager.persist(…)-Method is called and when a entityManager.merge(…) is called in spring data jpa. According to documentation: If the entity has not been

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 14:46

    here is the impl of save method(in SimpleJpaRepository):

    /*
         * (non-Javadoc)
         * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
         */
        @Transactional
        public  S save(S entity) {
    
            if (entityInformation.isNew(entity)) {
                em.persist(entity);
                return entity;
            } else {
                return em.merge(entity);
            }
        }
    

    So it looks at entityInformation.isNew(entity). Implementation of this method is(in AbstractPersistable) :

    public boolean isNew() {
    
            return null == getId();
        }
    

    So it decides based on id field

    0 讨论(0)
提交回复
热议问题