Update single field using spring data jpa
You can try something like this on your repository interface:
@Modifying
@Query("update EARAttachment ear set ear.status = ?1 where ear.id = ?2")
int setStatusForEARAttachment(Integer status, Long id);
You can also use named params, like this:
@Modifying
@Query("update EARAttachment ear set ear.status = :status where ear.id = :id")
int setStatusForEARAttachment(@Param("status") Integer status, @Param("id") Long id);
The int return value is the number of rows that where updated. You may also use void
return.
See more in reference documentation.
Hibernate offers the @DynamicUpdate annotation. All we need to do is to add this annotation at the entity level:
@Entity(name = "EARAttachment ")
@Table(name = "EARAttachment ")
@DynamicUpdate
public class EARAttachment {
//Code omitted for brevity
}
Now, when you use EARAttachment.setStatus(value)
and executing "CrudRepository" save(S entity)
, it will update only the particular field. e.g. the following UPDATE statement is executed:
UPDATE EARAttachment
SET status = 12,
WHERE id = 1