Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用spring数据jpa更新单个字段_Java_Spring_Spring Data_Updates_Spring Data Jpa - Fatal编程技术网

Java 使用spring数据jpa更新单个字段

Java 使用spring数据jpa更新单个字段,java,spring,spring-data,updates,spring-data-jpa,Java,Spring,Spring Data,Updates,Spring Data Jpa,我正在使用spring数据存储库——非常方便,但我遇到了一个问题。我可以轻松地更新整个实体,但我认为当我只需要更新单个字段时,这是毫无意义的: @Entity @Table(schema = "processors", name = "ear_attachment") public class EARAttachment { private Long id; private String originalName; private String uniqueName;/

我正在使用spring数据存储库——非常方便,但我遇到了一个问题。我可以轻松地更新整个实体,但我认为当我只需要更新单个字段时,这是毫无意义的:

@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;
要更新,我只需调用save方法。在日志中,我看到以下内容:

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 
我想看看这样的东西:

batching 1 statements: 1: update processors.ear_attachment set status=2 where id=1 
@Modifying
@Query("update EARAttachment ear set ear.status = :status where ear.id = :id")
int setStatusForEARAttachment(@Param("status") Integer status, @Param("id") Long id);

Spring的存储库有很多工具,可以使用名称约定来选择某些内容,可能有类似的更新工具,比如updateForStatus(int status)

您可以在存储库界面上尝试以下操作:

@Modifying
@Query("update EARAttachment ear set ear.status = ?1 where ear.id = ?2")
int setStatusForEARAttachment(Integer status, Long id);
还可以使用命名参数,如下所示:

batching 1 statements: 1: update processors.ear_attachment set status=2 where id=1 
@Modifying
@Query("update EARAttachment ear set ear.status = :status where ear.id = :id")
int setStatusForEARAttachment(@Param("status") Integer status, @Param("id") Long id);
int返回值是更新的行数。您也可以使用
void
return


请参阅文档中的详细信息。

Hibernate提供了@DynamicUpdate注释。我们只需在实体级别添加此注释:

@Entity(name = "EARAttachment ")
@Table(name = "EARAttachment ")
@DynamicUpdate
public class EARAttachment {
    //Code omitted for brevity
}
现在,当您使用
eartachment.setStatus(value)
并执行“crudepository”
save(S entity)
时,它将只更新特定字段。e、 g.执行以下更新语句:

UPDATE EARAttachment 
SET    status = 12,
WHERE  id = 1

您可以更新use databind以映射@PathVariable T entity和@RequestBody映射body。并更新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();
            }
        }
    });
}
publicstaticvoidapplychanges(对象实体、映射映射、字符串[]ignoreFields){
map.forEach((键,值)->{
如果(!Arrays.asList(ignoreFields).contains(key)){
试一试{
方法getMethod=entity.getClass().getMethod(getMethodNameByPrefix(“get”,key));
方法setMethod=entity.getClass().getMethod(getMethodNameByPrefix(“set”,key),getMethod.getReturnType());
调用(实体、值);
}捕获(IllegalAccessException | NoSuchMethodException | InvocationTargetException e){
e、 printStackTrace();
}
}
});
}

谢谢,现在开始工作,但我想知道它是否比使用entityManager.CreateQuery(..)更好?我在找一种类型的东西safe@DmitriiBorovoi如果我没记错的话,这也将调用
em.createQuery
,同样的事情也是如此。如果您想要某种类型安全的东西,那么您需要一种方法来知道为构建查询修改了哪些属性。在此之后,您可以使用自定义存储库来执行查询()。请问,如果我不使用@DynamicUpdate,将执行什么语句?如果我在setter之后保存()它还会处理Update语句吗?@AndreyM.Stepanov是的,将生成Update SQL。还要注意,
@DynamicUpdate
有一些性能开销,如中所述。仅在列数较大时使用。这种方法最大的缺点是需要先选择并绑定实体,然后才能更新其值。对的