Search code examples
javapostgresqlhibernatejpaopenjpa

Open JPA equivalent for Hibernate @ColumnTransformer


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


Solution

  • I am not sure of OpenJPA specific capabilities related to this, but the following two alternatives would work for all JPA providers:

    1. Create an updatable view that does the necessary transformations and map the entity to the view instead of the table.
    2. Move the transformations to middleware and apply them in entity lifecycle callbacks.

    The other benefit of both solutions is that you keep the entities clean of custom native SQL.