如何获得与使用 Java 使用的 JPA 相同的数据库连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7977146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the same database connection as JPA used using Java?
提问by Rinkal Bhanderi
I am using Jasper Reports for report generation.
我正在使用 Jasper Reports 生成报告。
I am using temporary tables to generate reports for which I need the same connection used by JPA while creating temporary table how do I achieve the same.
我正在使用临时表来生成报告,我需要在创建临时表时使用与 JPA 相同的连接,我如何实现相同的连接。
回答by Kowser
Probably you are just three steps away.
可能你就在三步之外。
You can do it using following way
您可以使用以下方式做到这一点
use JTA datasource for persistance.xml like as below
<persistence-unit name="sample"> <jta-data-source>java:comp/env/jdbc/DBConnectionDS</jta-data-source> .... </persistence-unit>
For report generation, retrieve the
connection
from datasource as shown belowInitialContext initialContext = new InitialContext(); DataSource dataSource = (DataSource)initialContext.lookup("java:comp/env/jdbc/DBConnectionDS"); Connection connection = dataSource.getConnection();
Use the connection to generate report something like below:
JasperPrint print = JasperFillManager.fillReport(report, parameters, connection);
将 JTA 数据源用于persistance.xml,如下所示
<persistence-unit name="sample"> <jta-data-source>java:comp/env/jdbc/DBConnectionDS</jta-data-source> .... </persistence-unit>
对于报告生成,
connection
从数据源中检索如下所示InitialContext initialContext = new InitialContext(); DataSource dataSource = (DataSource)initialContext.lookup("java:comp/env/jdbc/DBConnectionDS"); Connection connection = dataSource.getConnection();
使用连接生成如下报告:
JasperPrint print = JasperFillManager.fillReport(report, parameters, connection);
This should be all I believe. The idea is, using common JNDI connection for both, JPA & JasperReport, and then use them where applicable.
这应该是我所相信的。这个想法是,对 JPA 和 JasperReport 使用公共 JNDI 连接,然后在适用的地方使用它们。
I didn't work with JasperReports, but worked with BIRT Report and solved it this way without any problem.
我没有使用 JasperReports,但使用 BIRT Report 并以这种方式解决了它,没有任何问题。
回答by Marc Nuri
Within JasperReports you can use either native JDBC queries or EJBQL queries.
在 JasperReports 中,您可以使用本机 JDBC 查询或 EJBQL 查询。
When using the latter, your code should look like this (from JRJpaQueryExecuterapi):
使用后者时,您的代码应如下所示(来自JRJpaQueryExecuterapi):
Map parameters = new HashMap();
EntityManager em = emf.createEntityManager();
parameters.put(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER, em);
JasperRunManager.runReportToPdfFile(fileName, parameters);
If you really need the underlaying jdbc connection the way to achieve it varies depending on the JPA implementation you are using.
如果您确实需要底层 jdbc 连接,那么实现它的方式取决于您使用的 JPA 实现。
EclipseLink(JPA 2.0):
EclipseLink(JPA 2.0):
entityManager.getTransaction().begin();
java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
entityManager.getTransaction().commit();
(You won't need to begin and commit transactions for reporting)
(您不需要开始和提交交易以进行报告)