首页 > 解决方案 > Springboot JPA - 从数据库中检索实体时调用属性上的函数

问题描述

我正在为我的应用程序使用带有休眠功能的 Spring Boot。我的应用程序有一些敏感数据,因此它们以加密形式存储在数据库中。

但是,我的应用程序希望它们被解密。通常这是通过数据库中的过程完成的。在普通的旧 SQL 中,这可以毫无问题地完成。

@Entity
@Table(name = "my_table")
@Data
@Component
public class myObject implements Serializable
{
    //This is stored in DB decrypted. I want this to be decrypted when I fetch the records.
    @Column(name = "sensitive_data")
    private String sensitiveData;

    //Other unrelated properties
}

我可以在将对象加载到我的应用程序后进行解密,如下所示。但是,这将导致我需要的每个对象的数据库调用越来越多。这也会减慢我的应用程序的速度。所以我想在从数据库加载我的对象时找到一种方法来获取解密值。

@Repository
public interface myRepo extends JpaRepository<myObject , String>
{
    //(1)I use this for getting the list of objects from database
    @Query("SELECT obj FROM myObject obj WHERE obj.statusCode =:status_code ")
    List<myObject> getmyObjects(@Param("status_code") String status_code);

    //(2)I use this to get decrypted value after when I perform operations on objects received from (1)
    @Procedure("decrypt_function")
    String getDecrptedValue(@Param("encrypted") String encrypted);
}

需要明确的是,sensitive_data列将具有类似“212AFD232323DGGFF”的值。decrypt_function但是我需要在从数据库加载对象时调用解密的值。

有没有办法做到这一点?提前致谢。:)

标签: javaspring-boothibernatejpa

解决方案


推荐阅读