简体   繁体   English

如何在 sping Data JPA 中使用 CriteriaUpdate 执行更新操作?

[英]How to perform update operation using CriteriaUpdate in sping Data JPA?

As spring data JPA provide specification to get result like below由于弹簧数据 JPA 提供规范以获得如下结果

public static Specification<Customer> customerHasBirthday() {
    return new Specification<Customer> {
        public Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb) {
            return cb.equal(root.get(Customer_.birthday), today);
        }
    };
}

and then we can pass above specification in repository method customerRepository.findAll(customerHasBirthday);然后我们可以在存储库方法customerRepository.findAll(customerHasBirthday);传递上述规范customerRepository.findAll(customerHasBirthday); Now I want to perform update operation using criteria update in spring data JPA like below现在我想在 spring 数据 JPA 中使用标准更新来执行更新操作,如下所示

public static Specification<Customer> upadteCustomerName() {
    return new Specification<Customer> {
        public Predicate toPredicate(Root<Customer> root, CriteriaQuery query, CriteriaBuilder cb) {
            CriteriaUpdate<Customer> update = cb.createCriteriaUpdate(Customer.class)
            update.set("name", "newName");
            return update.where(cb.greaterThanOrEqualTo(root.get("id"), 2));
        }
    };
}

Obviously I will get compile time error as显然我会得到编译时错误

CriteriaUpdate can not be converted into predicate CriteriaUpdate 不能转换为谓词

Is spring data JPA provide any easy way to implement CriteriaUpdate and then directly pass the instance to Repository method like in Get operation we have findAll() method? Spring Data JPA 是否提供了任何简单的方法来实现 CriteriaUpdate 然后直接将实例传递给 Repository 方法,就像在 Get 操作中我们有findAll()方法一样?

or is there any other best way to implement criteriaUpdate in spring Data JPA?或者有没有其他最好的方法在 spring Data JPA 中实现标准更新?

Based on this answer you can do like this:基于这个答案,你可以这样做:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaUpdate<Customer> q = cb.createCriteriaUpdate(Customer.class);
Root<Customer> root = q.from(Customer.class);
q.set(root.get("name"),  "newName")
        .where(cb.greaterThanOrEqualTo(root.get("id"), 2)); 

int result = em.createQuery(q).executeUpdate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Sping Data Jpa OneToMany嵌套模型在Spring Boot Restful API上执行CURD操作? - How to do the CURD operations on Spring Boot Restful API using Sping Data Jpa OneToMany nested model? 使用 JPA CriteriaUpdate 从关系更新值 - updating a value from relation using JPA CriteriaUpdate 如何在 sping jpa 中使用转换器 - How to use to converter in sping jpa 如何使用 CriteriaUpdate 编写连接? - How to write Joins using CriteriaUpdate? JPA使用CriteriaUpdate将元素添加到集合中 - JPA add element to collection using CriteriaUpdate 如何使用 Sping JDBCTemplate 在 HashMap 中获取“键”和“值”列数据 - How to get 'key' and 'value' column data in HashMap using Sping JDBCTemplate 使用selectCase的JPA CriteriaUpdate插入十六进制 - JPA CriteriaUpdate with selectCase inserts HEX 如何在Sping JPA的同一列上使用多个LIKE&#39;%keyword%&#39;? - How to use multiple LIKE '%keyword%' in Sping JPA on same column? 使用spring数据jpa的spring boot。 如何从请求主体执行实体的部分更新? - Spring boot with spring data jpa. How can i perform partial update of entity from request body? 使用CriteriaUpdate查询时如何避免硬编码attributeName? - How to avoid hard code attributeName when using CriteriaUpdate Query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM