I am switching ORM framework from Hibernate to OpenJPA.
In Hibernate we could annotate a field with @ColumnTransformer like below.
@Column(name = "EMP_NAME", length = 4000)
@ColumnTransformer(
read = "pgp_pub_decrypt(emp_name::bytea,dearmor('"+key1+"'))",
write = "pgp_pub_encrypt(?, dearmor('"+key2+"'))"
)
private String empName;
How to do the same in OpenJPA
I am not sure of OpenJPA specific capabilities related to this, but the following two alternatives would work for all JPA providers:
The other benefit of both solutions is that you keep the entities clean of custom native SQL.