Transparent encryption/decryption
If your database supports transparent encryption/decryption, that's probably the best thing to do.
Jasypt
Another option is to use you can use Jasypt, which offers a wide range of Hibernate Types to encrypt/decrypt data.
Using a @ColumnTransformer
A very easy way to encrypt/decrypt an entity attribute is to use a @ColumnTransformer like this:
@ColumnTransformer(
read = """
pgp_sym_decrypt(
storage,
current_setting('encrypt.key')
)
""",
write = """
pgp_sym_encrypt(
?,
current_setting('encrypt.key')
)
"""
)
@Column(columnDefinition = "bytea")
private String storage;
This way, Hibernate will be able to encrypt the entity attribute when you persist or merge it and decrypt it upon fetching the entity.