JPA and SQL Server column encryption

别来无恙 提交于 2020-01-16 18:38:27

问题


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?


回答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();


来源:https://stackoverflow.com/questions/10323418/jpa-and-sql-server-column-encryption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!