Java 使用 spring 数据 jpa 更新单个字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29202277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update single field using spring data jpa
提问by Dmitrii Borovoi
I'm using spring-data's repositories - very convenient thing but I faced an issue. I easily can update whole entity but I believe it's pointless when I need to update only a single field:
我正在使用 spring-data 的存储库 - 非常方便,但我遇到了一个问题。我可以轻松地更新整个实体,但我相信当我只需要更新一个字段时它毫无意义:
@Entity
@Table(schema = "processors", name = "ear_attachment")
public class EARAttachment {
private Long id;
private String originalName;
private String uniqueName;//yyyy-mm-dd-GUID-originalName
private long size;
private EARAttachmentStatus status;
to update I just call method save. In log I see the followwing:
更新我只是调用方法保存。在日志中,我看到以下内容:
batching 1 statements: 1: update processors.ear_attachment set message_id=100,
original_name='40022530424.dat',
size=506,
status=2,
unique_name='2014-12-16-8cf74a74-e7f3-40d8-a1fb-393c2a806847-40022530424.dat'
where id=1
I would like to see some thing like this:
我想看到这样的事情:
batching 1 statements: 1: update processors.ear_attachment set status=2 where id=1
Spring's repositories have a lot of facilities to select something using name conventions, maybe there is something similar for update like updateForStatus(int status);
Spring 的存储库有很多工具可以使用名称约定来选择某些内容,也许有类似 updateForStatus(int status); 之类的更新功能。
采纳答案by Bruno César
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.
int 返回值是更新的行数。您也可以使用void
返回。
See more in referencedocumentation.
在参考文档中查看更多信息。
回答by Vijai
Hibernate offers the @DynamicUpdate annotation. All we need to do is to add this annotation at the entity level:
Hibernate 提供了 @DynamicUpdate 注释。我们需要做的就是在实体层面添加这个注解:
@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:
现在,当您使用EARAttachment.setStatus(value)
和执行 "CrudRepository" 时save(S entity)
,它只会更新特定字段。例如,执行以下 UPDATE 语句:
UPDATE EARAttachment
SET status = 12,
WHERE id = 1
回答by Fabricio Souza
You can update use databind to map @PathVariable T entity and @RequestBody Map body. And them update body -> entity.
您可以更新使用数据绑定来映射 @PathVariable T 实体和 @RequestBody 映射主体。然后他们更新 body -> entity。
public static void applyChanges(Object entity, Map<String, Object> map, String[] ignoreFields) {
map.forEach((key, value) -> {
if(!Arrays.asList(ignoreFields).contains(key)) {
try {
Method getMethod = entity.getClass().getMethod(getMethodNameByPrefix("get", key));
Method setMethod = entity.getClass().getMethod(getMethodNameByPrefix("set", key), getMethod.getReturnType());
setMethod.invoke(entity, value);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
}
});
}