Spring Data JPA仅在不存在时保存

hsa*_*dik 6 java spring-data-jpa

我在项目中找到了代码。看起来像:

private <S, T extends BaseEntity<S>> void saveIfNotExist(T baseEntity, JpaRepository<T, S> jpaRepository) {
        jpaRepository
                .findById(baseEntity.getId())
                .orElseGet(() -> jpaRepository.save(baseEntity));
}
Run Code Online (Sandbox Code Playgroud)

据我了解,如果实体已经存在,此方法不会在数据库中保存/更新实体。

BaseEntity:

public interface BaseEntity<T> {
    T gettId();
}
Run Code Online (Sandbox Code Playgroud)

执行:

@Entity
@Table(..)
public class Account implements BaseEntity<String> {

    @Id
    private String login;
    
    @Column(name = "is_archive")
    private Boolean isArchive;
   

    public String getId() {
        return this.getLogin();
    }
}
Run Code Online (Sandbox Code Playgroud)

存储库:

@Repository
public interface AccountRepository extends JpaRepository<Account, Integer>
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何用常见的 Spring Data JPA 实现替换此方法(仅在不存在时保存,不更新)?