java - 在 Java 实体上调用 DB 函数
问题描述
我尝试使用@Formula,但结果在数据库中为空。所以我的问题是我们如何在@PrePersist 或@Formula 中使用某些功能?我们有其他解决方案吗?
@Id
@JsonProperty("id")
private String id;
@Formula(value = "someschema.somefunction(`name`)")
@JsonProperty("name")
private String name;
@PrePersist
protected void onCreate(){
this.id = String.valueOf(UUID.randomUUID());
}
解决方案
答案是
- 我们不能
@Formula
使用函数插入数据,例如
@Modifying
@Query(value = "insert into customer " +
"(name, description, phone, id) " +
"VALUES (somefunction.encode_user(:name, 10, 'AES256', 'T', 'C'), " +
":description, " +
":phone, " +
":id)", nativeQuery = true)
@Transactional
void insertCustomer(
@Param("id") String id,
@Param("name") String Name,
@Param("description") String Description,
@Param("phone") String Phone
);
- 我们只能
@Formula
用于查询功能目的。
推荐阅读
- excel - Excel - 如果第一列匹配,则从其他工作表复制行数据
- geolocation - 通过 lat/lng 解析国家 + 州
- linux - 有没有办法在 exec.Start() 之后和命令开始之前加载 seccomp 过滤器
- playframework - 如何使用 h2-browser 查看 play 2.7 H2 数据库的内容?
- django - 部署在 Kubernetes 上时是否需要在反向代理后面运行 Django
- jolokia - 默认情况下启用许多模块的 Metricbeat
- python - 如何处理 API 请求中的不同响应码?
- c# - 无法导入 Visual Studio 2019 Scoped Credential
- unix - UNIX 中的紧急功能
- javascript - 如何根据用户纬度和经度显示最近位置列表