0

I'm using WebSphere 7 and their JPA 2.0 implementation which is based on OpenJPA, and I have something driving me crazy. I have to connect to a SQL Server 2008 database that uses the database column encryption. The encryption is done by several database commands:

1 - OPEN SYMMETRIC KEY DECRYPTION BY CERTIFICATION

2 - Perform insert/select/update/etc using the database methods EncryptByKey or DecryptByKey

3 - CLOSE SYMMETRIC KEY

I have searched and it does not appear that OpenJPA supports this functionality. Does anybody know how to get OpenJPA to play nicely with this type of encryption? Or should I just skip JPA for this project and use good old fashioned PreparedStatements?

bnorwood
  • 93
  • 1
  • 11

1 Answers1

1

So yeah, it does look like doing a native query is only way to do this. So it comes out to something like this:

EntityManager em = getEntityManager();
Query openKey = em.createNativeQuery("OPEN SYMMETRIC KEY MY_KEY  DECRYPTION BY CERTIFICATE MY_CERT");
openKey.executeUpdate();

Query query = em.createNativeQuery("SELECT FIRSTNAME, LASTNAME, CONVERT(varchar, DECRYPTBYKEY(SSN)) as SSN from report where record_id = ?", Report.class);
query.setParameter(1, recordId);
report = (Report) query.getSingleResult();

Query closeKey = em.createNativeQuery("CLOSE SYMMETRIC KEY MY_KEY");
closeKey.executeUpdate();
bnorwood
  • 93
  • 1
  • 11