gpt4 book ai didi

orm - 更新 JPA CriteriaUpdate 中的现有列值

转载 作者:行者123 更新时间:2023-12-04 11:36:22 24 4
gpt4 key购买 nike

我有一个实体用户,我的用户数据库值为:

name     totalScore
--------------------
ABC 25
XYZ 30

现在我想运行查询
update user set totalScore=totalScore+ (2*totalScore);

我们如何通过 JPA 2.1 CriteriaUpdate 实现它???
CriteriaBuilder criteriaBuilder = em().getCriteriaBuilder();
//Updates the salary to 90,000 of all Employee's making more than 100,000.
CriteriaUpdate update = criteriaBuilder.createCriteriaUpdate(User.class);
Root user = update.from(User.class);
Expression<Long> abc=user.get("totalScore");

update.set("totalScore", ?? );
// what expression is here to be used to replace old value with new
Query query = em().createQuery(update);
int rowCount = query.executeUpdate();

最佳答案

您可以使用此代码。它会为你工作

// Gives all Employees a 10% raise.

CriteriaUpdate update = criteriaBuilder.createCriteriaUpdate(Employee.class);

Root employee = update.from(Employee.class);

update.set(employee.get("salary"),criteriaBuilder.sum(employee.get("salary"), criteriaBuilder.quot(employee.get("salary"), 10));

Query query = entityManager.createQuery(update);
int rowCount = query.executeUpdate();

关于orm - 更新 JPA CriteriaUpdate 中的现有列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31186131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com