如何从Java EE 6中的JPA2 EntityManager获取DataSource或Connection [英] How to get DataSource or Connection from JPA2 EntityManager in Java EE 6

查看:232
本文介绍了如何从Java EE 6中的JPA2 EntityManager获取DataSource或Connection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作应用程序,我使用Java EE 6和EclipseLink进行持久化和PostgreSQL数据库。

I have a working application where I use Java EE 6 with EclipseLink for persistence and a PostgreSQL database.

对于用户注册我想设置密码PostgreSQL:

For the User-Registration I want to set the password in PostgreSQL to:

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

由于我不能使用DigestUtils,我必须手动将用户插入数据库。
为了保持我的应用程序可配置,我不想使用
InitialContextInstance.lookup(dataSource)查询DataSource,而是提取它(或连接) )以某种方式从EntityManager
喜欢:

As I cant use DigestUtils for this, I have to insert the user manually into the DB. To keep my application configurable I do not want to query the DataSource with an InitialContextInstance.lookup(dataSource) but to extract it (or the Connection) somehow from the EntityManager like:

DataSource ds = entityManagerInstance.someFunctionThatReturnsADataSourceOrConnection();

或者可以使用createNativeQuery或类似的东西在
中使用预先准备好的语句防止注射?

Or would it be possible to use createNativeQuery or something similar in conjuntion with a prepared statement to protect against injections?

推荐答案

有时它只需要在谷歌再次运行:

sometimes it just takes another run in google:

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

Eclipse链接文档

这篇关于如何从Java EE 6中的JPA2 EntityManager获取DataSource或Connection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆