Video in progress

In a Hibernate-based system that stores personal data (passport, phone, address), how do you design the persistence layer: encryption, right to erasure, logs?

First I map out which data lives where and move the personal fields into a separate table or columns. I encrypt the necessary fields at the application level (keys in a KMS), add a blind index for searching, design an anonymization path for deletion, and close the leaks through SQL parameter logs, the L2 cache and backups.

📖 Answer

The technical decisions start from the legal requirements: which data is personal, how long it is kept, who can see it, and what happens when a user asks for deletion. Uzbekistan's personal data law and GDPR both ask these questions.

At the model level:

  • I don't scatter personal data across domain tables. customer_pii is a separate table (or a separate schema), and other tables refer to it only by customer_id. This lets you manage access rights, encryption and deletion in one place;
  • encryption at the application level: AES-GCM inside an AttributeConverter, keys in KMS/Vault, and the key version is stored inside the encrypted value (for rotation). The database and the backups only ever see encrypted bytes;
  • for a field you need to search on (the phone), an HMAC in a separate column: a blind index. Exact-match search works, like does not, and I agree on that with the product people up front.

Right to erasure: a real DELETE is often impossible (financial records must be kept by law). So it's "anonymization": delete the row in the PII table or replace the values, while the linked business records stay. With encryption there is another way: a separate key per user, and destroying the key on deletion (crypto-shredding).

Leak channels specific to Hibernate:

  • the org.hibernate.orm.jdbc.bind TRACE log writes parameter values: keep it off on prod and mask in the log pipeline;
  • the L2 cache and the application cache: don't cache entities with personal data, or cache them encrypted;
  • Envers audit tables: they are also a copy of the PII and have to be included in the deletion process;
  • toString()/Lombok: the whole entity ends up in exceptions and logs.

Trade-off: encryption makes search, reporting and debugging harder, and key management is a new operational risk. So I encrypt not everything but the fields on an explicit list, and protect the rest with access rights and auditing.