【问题标题】:Call DB Function on Java Entity在 Java 实体上调用 DB 函数
【发布时间】:2021-07-16 05:16:15
【问题描述】:

我尝试使用@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());
}

【问题讨论】:

    标签: java postgresql spring-boot hibernate jpql


    【解决方案1】:

    答案是

    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 用于查询功能目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多