2012-04-18 45 views
1

我在项目中使用Hibernate 3.2.5,Jasper 3.7.2为应用程序生成一些报告。Hibernate获取JasperRunManager的连接对象

有什么办法从HibernateSessionFactory对象中获取连接对象?我只想处理一个连接,因为此时我必须有一个ODBC连接的属性文件,它将通过JasperRunManager静态方法来处理Jasper,另一方面还有Hibernate。

这是我需要通过连接的方法:

byte[] net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(InputStream inputStream, Map parameters, Connection conn) throws JRException 

在此先感谢。 :)

回答

3

我曾与休眠4 同样的问题,这里是解决

Session ses = ... 
final InputStream finalCompiledReportStream = compiledReportStream; 
final OutputStream finalByteArrayOutputStream = byteArrayOutputStream; 
ses.doWork(new Work() { 
    public void execute(Connection connection) throws SQLException { 
     try { 
      JasperFillManager.fillReportToStream(finalCompiledReportStream, finalByteArrayOutputStream, parameters, connection); 
     } catch (JRException e) { 
      ReportAction.this.logger.error(e); 
     } 
    } 
}); 
0

这个工作赫然并且可以使用连接对象的其他地方,你可能需要在代码中。 doWork可能需要你保持这么多次,但创建一个连接对象nice是一个更干净的解决方案。

SessionImpl sessionImpl = (SessionImpl) session; 
Connection conn = sessionImpl.connection(); 

哪里session是Hibernate的Session对象的名称