JPA: update only specific fields

前端 未结 7 2049
自闭症患者
自闭症患者 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条回答
  •  执笔经年
    2020-12-02 14:46

    You are able to write something like

    @Modifying
        @Query("update StudentXGroup iSxG set iSxG.deleteStatute = 1 where iSxG.groupId = ?1")
        Integer deleteStudnetsFromDeltedGroup(Integer groupId);
    

    Or If you want to update only the fields that were modified you can use annotation

    @DynamicUpdate
    

    Code example:

    @Entity
    @Table(name = "lesson", schema = "oma")
    @Where(clause = "delete_statute = 0")
    @DynamicUpdate
    @SQLDelete(sql = "update oma.lesson set delete_statute = 1, "
            + "delete_date = CURRENT_TIMESTAMP, "
            + "delete_user = '@currentUser' "
            + "where lesson_id = ?")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    

提交回复
热议问题