JPA: update only specific fields

前端 未结 7 2072
自闭症患者
自闭症患者 2020-12-02 14:02

Is there a way for updating only some fields of an entity object using the method save from Spring Data JPA?

For examp

7条回答
  •  旧时难觅i
    2020-12-02 14:46

    Using JPA you can do it this way.

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaUpdate criteria = builder.createCriteriaUpdate(User.class);
    Root root = criteria.from(User.class);
    criteria.set(root.get("lastSeen"), date);
    criteria.where(builder.equal(root.get("id"), user.getId()));
    session.createQuery(criteria).executeUpdate();
    

提交回复
热议问题