0

I am working on Spring MVC project with Hibernate. I have an table where some columns having encrypted data. Whenever I want to fetch data (unique result or list) I have to loop over the data and perform decryption logic then again set into models/entity.

Currently we are doing like :

            query = session.createSQLQuery("SELECT C.* FROM CITY C");
            query.addEntity(City.class);                
            List<City> list = query.list();
            for (City city : list)
            {
                city.setName(AESHelper.decrypt(city.getName(), "key"));
            }

can we have something in Hibernate by using we can apply such logic while hibernate is transforming data into entity, can we use ResultTransformer for this?

1 Answer 1

1

You can use ResultTransformer without doubt. But, if you have a lot of columns/entities which requires this then use Hibernate Interceptors.

In your example annotate name with custom annotation like below

City {
 @CustomAnnotation(decrypt = true) 
 private String name;
..
}

Within the interceptor when ever data is loaded, check the entities property for this custom annotation. If it is set to true then decrypt it.

This approach is non intrusive.

Example

Update: ResultTransformer

sess.createSQLQuery("SELECT name from City where name like 'xxyy'")
        .setResultTransformer(new ResultTransformer(){
   Object transformTuple(Object[] tuple, String[] aliases){
       // assign each tuple value to your object and return
   }

})
Sign up to request clarification or add additional context in comments.

3 Comments

I have only one column in my case. Can you show how can it be done using ResultTransformer.
@Amogh I think the resultTransformer returns nonMangedEntity bean, is that fine with you?
Yes, I will make an DTO class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.