首页 > 解决方案 > 在 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());
}

标签: javapostgresqlspring-boothibernatejpql

解决方案


答案是

  1. 我们不能@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
);
  1. 我们只能@Formula用于查询功能目的。

推荐阅读