2016-08-24 27 views
0

在我的應用程序中,我使用@Type註釋啓用了jasypt加密。但是當我需要在沒有任何加密的情況下部署應用程序時,我必須按照以下方式更改@Type註釋的屬性。目前我正在手動執行此操作。有沒有什麼辦法可以配置(根據配置值獲取@Type註釋的屬性)?謝謝。如何配置Hibernate @Type註釋的屬性

@Entity 
@Table 
public class Data { 

    @Id 
    private Integer id; 

    @Type(type = "encryptedString") // Need to enable for Encryption 
    @Type(type = "org.hibernate.type.TextType") // Need to enable for Non Encryption 
    private String data; 
} 
+1

JPA沒有這樣的'@Type'註解。顯然,Hibernate有一個'@Type'註釋,但那不是JPA。 –

+1

處理服務層中的加密,並通過例如Spring配置文件基於運行時環境交換Encryption類的不同實現。 –

+0

我已將問題帖子更正爲「如何配置Hibernate @Type註釋的屬性」。謝謝。 – Channa

回答

0

通過使用「JPA實體生命週期回調方法」,我將加密和解密作爲一個可配置的參數來處理。 現在Hibernate不負責加密和解密,應用程序相關的DTO本身明確進行加密和解密相關的操作。

@Entity 
@Table 
public class Data { 

    @Id 
    private Integer id; 

    @Type(type = "org.hibernate.type.TextType") 
    private String data; 

    // Before Persist or Update to Database 
    @PrePersist 
    @PreUpdate 
    void beforePersistOrUpdate() { 

     // Do encrypt 
     if(ProjectProperty.isEncryptionEnabled) { 
      this.data = ServiceUtil.commonService.doEncryptString(this.data); 
     } 
    } 

    // Before Load from Database 
    @PostLoad 
    void beforeLoad() { 

     // Do Decrypt 
     if(ProjectProperty.isEncryptionEnabled) { 
      this.data = ServiceUtil.commonService.doDecryptString(this.data); 
     } 
    } 
}