使用jpa时save的insert和update区分逻辑处理

package org.springframework.data.domain;

import org.springframework.lang.Nullable;

/**
 * Simple interface for entities.
 *
 * @param <ID> the type of the identifier
 * @author Oliver Gierke
 * @author Christoph Strobl
 */
public interface Persistable<ID> {

    /**
     * Returns the id of the entity.
     *
     * @return the id. Can be {@literal null}.
     */
    @Nullable
    ID getId();

    /**
     * Returns if the {@code Persistable} is new or was persisted already.
     *
     * @return if {@literal true} the object is new.
     */
    boolean isNew();
}

实现

Persistable接口
override 这个isNew方法。如果是new,就生成insert语句,否则生成update。
posted @ 2022-01-26 17:18  然然1907  阅读(1105)  评论(0编辑  收藏  举报