【问题标题】:Spring Data JPA check if record exist and update else insertSpring Data JPA检查记录是否存在并更新否则插入
【发布时间】:2021-09-13 23:03:59
【问题描述】:

如果使用一个参数的 Spring JPA 查询已经存在给定案例的记录,我如何检查数据库。如果它存在,它会进行更新,否则它会作为新记录插入。我尝试了一个简单的实现,但最终出现 500 服务器错误,没有记录其他错误。

已解决 [java.lang.NullPointerException] 已完成 500 INTERNAL_SERVER_ERROR

这是我迄今为止尝试过的 我的控制器

 @RequestMapping(path="/updatedm",method = RequestMethod.POST)
    public Boolean updateDMStatus(@RequestParam("country") String country,
                                 @RequestParam("Id") String pId,
                                 @RequestParam("case") String case,
                                 @RequestParam("status") String status,
                                  @RequestParam("updatedBy") String updatedBy){

        Boolean createDm = eaDataStoreService.updateDM(country,Id,case,status,updatedBy);

        return createDm;

    }

我的仓库

public interface DMValidatedRepository extends CrudRepository<DMValidated, String> {

    DMValidated findByCase(@Param("case") String case);
}

我的服务

 public boolean updateDM(String country, String Id, String case, String status,String updatedBy) {
        DMValidated document = dmValidated.findByCase(case);

        if(document != null){
            document.setStatus(status);
            document.setUpdatedBy(updatedBy);
            dmValidated.save(document);
        }else{
            document.getId();
            document.getCase();
            document.getCountry();
            document.getStatus();
            document.getUpdatedBy();
            dmValidated.save(document);
        }


        return true;

    }

我的模特

@Data
@ToString
@Entity
@Table(name = "DMStatus")
public class DMValidated{
    @Id
    @GeneratedValue
    private String id;

    @Column(name = "country")
    private String country;
    @Column(name = "Id")
    private String Id;
    @Column(name = "Case")
    private String case;
    @Column(name = "status")
    private String status;
    @Column(name = "updatedBy")
    private String updatedBy;

    public  DMValidated( String country, String Id,
                                String case, String status, String updatedBy){
        this.country = country;
        this.Id=Id;
        this.case = case;
        this.status =status;
        this.updatedBy = updatedBy;
    }

我不确定这是否是正确的做法,已尝试进行研究,但我没有找到具体的东西。我怎样才能做到这一点?

【问题讨论】:

    标签: java spring-boot hibernate jpa


    【解决方案1】:

    当新对象需要插入时,您刚刚忘记了正确创建对象的代码并不难

        if(document != null){
            document.setStatus(status);
            document.setUpdatedBy(updatedBy);
        }else{
            document = new DMValidated();
            document.setId(Id);
            document.setCase(case);
            document.setCountry(country);
            document.setStatus(status);
            document.setUpdatedBy(updatedBy);
        }
    
        dmValidated.save(document);
    

    您的代码中之前出现的错误如下

          }else{
                document.getId();
                ...
               }
    

    在这个else中只有document == null才会得到,所以当你调用document.getId()时会抛出空指针异常,然后出现500错误。

    【讨论】:

    • 谢谢@Boug
    【解决方案2】:

    您尝试执行的此操作通俗地称为 UPSERT,并且碰巧要完全使用 JPA 和 Hibernate 来实现有点挑战。我过去的做法是使用 jOOQ

    也就是说,StackOverflow 中有很好的资源可以帮助您在搜索这些关键字时更快地找到答案。

    总之,这里有一些你可能想先阅读的读物:

    Vlad Mihalcea 的几乎所有读物都会让您深入了解这个主题,以及 JPA 和 Hibernate。

    【讨论】:

    • 谢谢@ne3rmore,我会读,你没有工作示例吗?
    • 我看我要买,我用的是sql server
    【解决方案3】:
    else{
                document.getId();
                document.getCase();
                document.getCountry();
                document.getStatus();
                document.getUpdatedBy();
                dmValidated.save(document);
            }
    

    以上案例文档对象初始化但属性数据类型字符串始终为空 这就是为什么每次 document.getId() 为 null 或其他属性时都会发生 Nullpointer。

    正确的代码

     else{
            document = new DMValidated();
            document.setId(Id);
            document.setCase(case);
            document.setCountry(country);
            document.setStatus(status);
            document.setUpdatedBy(updatedBy);
        }
    
    

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 2016-02-29
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2010-12-10
      • 2012-10-09
      • 1970-01-01
      • 2015-12-18
      相关资源
      最近更新 更多