【问题标题】:JPA CrudRepository Disable New Element Insertion (Update Only)JPA CrudRepository 禁用新元素插入(仅更新)
【发布时间】:2022-06-22 22:00:32
【问题描述】:

我的用户表有以下存储库接口。

@Repository
public interface UserRepository extends CrudRepository<User, Long>

我想禁止将新行插入到用户表中,并使用保存功能仅更新现有条目(如果有的话)。我怎样才能做到这一点?有没有可以传递到存储库界面的选项?或者当没有匹配的实体时我应该重写保存方法不做任何事情?

【问题讨论】:

    标签: java jpa spring-data-jpa


    【解决方案1】:

    您可以在实体映射上进行设置。

    例如:

    @Entity
    public class User {
    
      @Id 
      @Column(insertable=false)
      private Integer id;
    
      @Column(insertable=false)
      private String name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 2019-03-23
      • 2011-03-19
      • 1970-01-01
      相关资源
      最近更新 更多