How to get datasource or connection from JPA2 entitymanager in Java EE 6

I have a working application that I use Java EE 6 with eclipse link persistence and a PostgreSQL database

For user registration, I want to set the password in PostgreSQL to:

... password = crypt('inputPassword',gen_salt('bf')) ...

Because I can't use digestutils, I have to manually insert users into the database In order to keep my application configurable, I don't want to use a query datasource initialcontextinstance Lookup (datasource), but extracting it (or connection) from the entitymanager likes:

DataSource ds = entityManagerInstance.someFunctionThatReturnsADataSourceOrConnection();

Or can you use createnativequery or similar combination prepared declarations to prevent injection?

Solution

Sometimes it just needs another running on Google:

entityManager.getTransaction().begin();
java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
entityManager.getTransaction().commit();

As described in eclipse link documentation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>