【问题标题】:CriteriaUpdate append to existing string value in postgres in JPACriteriaUpdate 附加到 JPA 中 postgres 中的现有字符串值
【发布时间】:2022-12-13 21:58:09
【问题描述】:

在更新列的值(Postgres 中的文本类型)时,我想将新值附加到现有值。我正在尝试使用 CriteriaUpdate 来实现它,但不确定如何实现它。

员工表实体

@Column(name = "warning_message")
String warningMessage;

JPA代码

CriteriaUpdate<EmployeeTblEntity> criteriaUpdate = builder.createCriteriaUpdate(EmployeeTblEntity.class);

criteriaUpdate.set(root.get("warningMessage"),  employeeTblEntity.getWarningMessage());

上面的代码在 warning_message 中设置了一个新值,我想要的是将新值附加到现有值。

【问题讨论】:

  • 我提供了一个答案,如果它满足要求,请考虑点赞并采纳为答案,否则请随时发表评论。

标签: hibernate jpa spring-data-jpa


【解决方案1】:

您问

如何将新值附加到现有值。(然后用 CriteriaUpdate 更新它)

您可以使用 concat() api 将新的字符串值附加到所需的选定路径,除此之外,许多其他操作和聚合可以通过 CriteriaBuilder 根接口针对不同的路径类型实现。

CriteriaUpdate<T> criteriaUpdate = criteriaBuilder.createCriteriaUpdate(type);
Root<T> updateRoot = criteriaUpdate.from(type);
Path<String> path = updateRoot.get(update_column);
criteriaUpdate.set(path, criteriaBuilder.concat(updateRoot.get(desired_column), "value"));

在您的情况下,它将如下所示

CriteriaUpdate<EmployeeTblEntity> criteriaUpdate = builder.createCriteriaUpdate(EmployeeTblEntity.class);
Path<String> path = root.get("warningMessage");
criteriaUpdate.set(path,  criteriaBuilder.concat(path, employeeTblEntity.getWarningMessage()));
...

【讨论】:

    猜你喜欢
    • 2015-09-20
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 2023-02-11
    • 2011-04-15
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多