更新查询期间的条件生成器错误:BinaryArithmeticOperation 与预期类型不匹配
Criteria builder error during update query: BinaryArithmeticOperation did not match expected type
我正在尝试使用休眠条件生成器执行下一个 SQL:
update account set balance = balance + 500;
这是我的查询建筑代码:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaUpdate<JBCAccount> update = cb.createCriteriaUpdate(JBCAccount.class);
Root root = update.from(JBCAccount.class);
update.set("balance", cb.sum(root.get("balance"), amount));
getEntityManager().createQuery(update).executeUpdate();
我也试过了:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaUpdate<JBCAccount> update = cb.createCriteriaUpdate(JBCAccount.class);
Root root = update.from(JBCAccount.class);
ParameterExpression<Long> amountExpression = cb.parameter(Long.class);
update.set("balance", cb.sum(root.get("balance"), amountExpression));
getEntityManager().createQuery(update)
.setParameter(amountExpression, amount).executeUpdate();
两次执行都会产生下一个错误:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [org.hibernate.query.criteria.internal.expression.BinaryArithmeticOperation@53e0be2d] did not match expected type [java.lang.Long (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [org.hibernate.query.criteria.internal.expression.BinaryArithmeticOperation@53e0be2d] did not match expected type [java.lang.Long (n/a)]] with root cause
"java.lang.IllegalArgumentException: Parameter value [org.hibernate.query.criteria.internal.expression.BinaryArithmeticOperation@53e0be2d] did not match expected type [java.lang.Long (n/a)]
MySQL 数据库中的余额类型是:balance bigint UNSIGNED
.
amount
代码中的变量类型为 Long
.
balance
属性 of JBCAccount
是 Long
.
我还尝试用 balance bigint
(即没有 UNSIGNED
)重新创建一个 MySQL 模式。还是一样的问题。
我错过了什么?
您必须指定路径属性指的是 Long:
update.set((Path<?>) root.<Long>get("balance"), cb.sum(root.<Long>get("balance"), amount));
我正在尝试使用休眠条件生成器执行下一个 SQL:
update account set balance = balance + 500;
这是我的查询建筑代码:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaUpdate<JBCAccount> update = cb.createCriteriaUpdate(JBCAccount.class);
Root root = update.from(JBCAccount.class);
update.set("balance", cb.sum(root.get("balance"), amount));
getEntityManager().createQuery(update).executeUpdate();
我也试过了:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaUpdate<JBCAccount> update = cb.createCriteriaUpdate(JBCAccount.class);
Root root = update.from(JBCAccount.class);
ParameterExpression<Long> amountExpression = cb.parameter(Long.class);
update.set("balance", cb.sum(root.get("balance"), amountExpression));
getEntityManager().createQuery(update)
.setParameter(amountExpression, amount).executeUpdate();
两次执行都会产生下一个错误:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [org.hibernate.query.criteria.internal.expression.BinaryArithmeticOperation@53e0be2d] did not match expected type [java.lang.Long (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [org.hibernate.query.criteria.internal.expression.BinaryArithmeticOperation@53e0be2d] did not match expected type [java.lang.Long (n/a)]] with root cause
"java.lang.IllegalArgumentException: Parameter value [org.hibernate.query.criteria.internal.expression.BinaryArithmeticOperation@53e0be2d] did not match expected type [java.lang.Long (n/a)]
MySQL 数据库中的余额类型是:balance bigint UNSIGNED
.
amount
代码中的变量类型为 Long
.
balance
属性 of JBCAccount
是 Long
.
我还尝试用 balance bigint
(即没有 UNSIGNED
)重新创建一个 MySQL 模式。还是一样的问题。
我错过了什么?
您必须指定路径属性指的是 Long:
update.set((Path<?>) root.<Long>get("balance"), cb.sum(root.<Long>get("balance"), amount));