Hibernate Converter and retrieve attribute name inside Converter


#1

We are trying to use hibernate Converter to encrypt / decrypt data stored via hibernate for few columns

@Convert(attributeName="myattr",converter=DataEncryptionConverter.class)
private String actualValue;

The plan is to implement reusable Converter based on data type (String, Date etc).

However the plan is to maintain a config template that indicates if the field encryption should be turned on / off

Approach

@Converter
public class DataEncryptionConverter implements AttributeConverter<String,String>{
    private static Logger logger = LoggerFactory.getLogger(DataEncryptionConverter.class);
    @Override
    public String convertToDatabaseColumn(String arg0) {
        logger.info("convertToDatabase>input:"+arg0);
        if(attributeName matches (TemplateList)) { 
           //check if the attributeName is part of approved TemplateList where data needs to be encrypted
           return encryptData(arg0);
        }
        else {
          return  arg0;
        }
   }
    @Override
    public String convertToEntityAttribute(String arg0) {
        logger.info("convertToEntity>input:"+arg0);
        if(attributeName matches (TemplateList)) { 
           //check if the attributeName  is part of approved TemplateList where data needs was encrypted
           return deCryptData(arg0);
        }
        else {
          return  arg0;
        }
    }

}

So, is there any way to retrieve the attributeName that is set as part of @Converter ?


#2

I don’t think you can do it with plain JPA convertors.

But you can do it with Hibernate custom Types which implement the DynamicParameterizedType interface. Then, you can get access to the underlying field type:

@Override
public void setParameterValues(Properties parameters) {
        arrayObjectClass = ((ParameterType) parameters.get(PARAMETER_TYPE)).getReturnedClass();
}

For more details, check out this article.


#3

The bigger problem really is that each usage of that converter uses the same converter instance. You’d either have to manually apply the converter (@Convert) as your initial line or use a custom type as Vlad mentions.


#4

Hi Vlad,

Thanks for the approach.

However, by using TypeDefs, we can convert to a particular type during persist. But, how can we decode the written data ? (Our scenario was to encrypt and decrypt the data using symmetrical algorithm)


#5

For encrypting and decrypting you can also use @ColumnTransformer.

Check out the Hibernate User Guide for an example.


#6

The documentation suggests that we can call db functions within columntransformer. Is it possible to use any external java api ?


#7

The @ColumnTransforner will adjust the SQL you send to the DB. You can’t use it to execute an external Java API. To do that, you can just use the data access layer.


#8

Hi vlad

We decided to use TypeDef for this purpose and used parameters to make it configurable. Thanks for the suggestion.


#9

You are very welcome.