<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 251  文章 - 504  trackbacks - 0
    <2006年6月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    本博客系個人收集材料及學(xué)習(xí)記錄之用,各類“大俠”勿擾!

    留言簿(14)

    隨筆分類

    收藏夾

    My Favorite Web Sites

    名Bloger

    非著名Bloger

    搜索

    •  

    積分與排名

    • 積分 - 202413
    • 排名 - 285

    最新評論

    注: 此方法使用簡單,但是沒有分頁讀取數(shù)據(jù),適合數(shù)據(jù)量不大情況下的報表

    原文:
    http://www.hibernate.org/79.html

    Using JasperReports with Hibernate
    This section will demonstrate a couple of ways to use the results of a Hibernate query as the datasource of a report in JasperReports.

    When the Hibernate query returns a simple collection of objects, The JRBeanCollectionDataSource class in the JasperReports library will work fine.

    --------------------------------------------------------------------------------------------------------
    List cats = session.find("from eg.Cat");

    Map parameters = new HashMap();
    parameters.put("Title", "The Cat Report");

    InputStream reportStream = this.class.getResourceAsStream("/the-cat-report.xml");
    JasperDesign jasperDesign = JasperManager.loadXmlDesign(reportStream);
    JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

    JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(cats);
    JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, ds);

    JasperManager.printReportToPdfFile(jasperPrint, "the-cat-report.pdf");

    --------------------------------------------------------------------------------------------------------

    ? However, when the Hibernate query returns tuples of objects (each tuple as an array, each array as an element in the returned List), things get a little tricky. Jasper needs a way to reference each object in the array by a field name. This class is a good solution, but at this time you are required to pass an array of field names matching the results of the query.

    --------------------------------------------------------------------------------------------------------
    public class HibernateQueryResultDataSource implements JRDataSource {

    private String[] fields;
    private Iterator iterator;
    private Object currentValue;

    public HibernateQueryResultDataSource(List list, String[] fields) {
    ? this.fields = fields;
    ? this.iterator = list.iterator();
    }

    public Object getFieldValue(JRField field) throws JRException {
    ? Object value = null;
    ? int index = getFieldIndex(field.getName());
    ? if (index > -1) {
    ? ? Object[] values = (Object[])currentValue;
    ? ? value = values[index];
    ? }
    ? return value;
    }

    public boolean next() throws JRException {
    ? currentValue = iterator.hasNext() ? iterator.next() : null;
    ? return (currentValue != null);
    }

    private int getFieldIndex(String field) {
    ? int index = -1;
    ? for (int i = 0; i < fields.length; i++) {
    ? ? if (fields
    .equals(field)) {
    ? ? index = i;
    ? ? break;
    ? ? }
    ? }
    ? return index;
    }

    }

    --------------------------------------------------------------------------------------------------------

    Now running your report would look something like this:


    --------------------------------------------------------------------------------------------------------
    List cats = session.find("select cat.type, cat.birthdate, cat.name from eg.DomesticCat cat");

    Map parameters = new HashMap();
    parameters.put("Title", "The Cat Report");

    InputStream reportStream = this.class.getResourceAsStream("/the-cat-report.xml");
    JasperDesign jasperDesign = JasperManager.loadXmlDesign(reportStream);
    JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

    String[] fields = new String[] { "type", "birthdate", "name"};
    HibernateQueryResultDataSource ds = new HibernateQueryResultDataSource(cats, fields);
    JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, ds);

    JasperManager.printReportToPdfFile(jasperPrint, "the-cat-report.pdf");

    --------------------------------------------------------------------------------------------------------


    Another, alternate implementation, from Erik Romson:


    --------------------------------------------------------------------------------------------------------
    public class HibernateQueryResultDataSource implements JRDataSource
    {
    ? private static final transient Logger logger =
    ? ? Logger.Factory.getInstance(HibernateQueryResultDataSource.class);
    ? protected HashMap fieldsToIdxMap=new HashMap();
    ? protected Iterator iterator;
    ? protected Object currentValue;
    ? List values;

    ? public HibernateQueryResultDataSource(List list, String query)
    ? {
    ? ? int start =query.indexOf("select ");
    ? ? int stop =query.indexOf(" from ");
    ? ? Assertion.assertTrue(
    ? ? ? (start!=-1) &&
    ? ? ? (stop!=-1),
    ? ? ? "The query "+
    ? ? ? query+
    ? ? ? " must be of the form select x,x from ..."
    ? ? );
    ? ? start+="select".length();
    ? ? String parameters=query.substring(start,stop);
    ? ? parameters=parameters.trim();
    ? ? Assertion.assertTrue(
    ? ? ? ? parameters.length()>0,
    ? ? ? ? "The query "+
    ? ? ? ? query+
    ? ? ? ? " seems to be weird"
    ? ? );
    ? ? StringTokenizer tokenizer=new StringTokenizer(parameters,",");
    ? ? int idx=0;
    ? ? while( tokenizer.hasMoreTokens() )
    ? ? {
    ? ? ? ? String parameter=tokenizer.nextToken();
    ? ? ? ? fieldsToIdxMap.put( parameter.trim(), new Integer(idx) );
    ? ? ? ? idx++;
    ? ? }
    ? ? values=list;
    ? ? this.iterator = list.iterator();
    ? }
    public Object getFieldValue(JRField field) throws JRException
    ? {
    ? ? String fieldName=field.getName().replace('_','.');
    ? ? //JasperReports does not allow '.' in field names, so we
    ? ? //use '_' for nested properties instead and must convert here
    ? ? //(comment added by kbrown)
    ? ? Integer idxInt=(Integer) fieldsToIdxMap.get(fieldName);
    ? ? if (idxInt==null)
    ? ? {
    ? ? ? ? Assertion.fail(
    ? ? ? ? ? "The field \""+
    ? ? ? ? ? fieldName+
    ? ? ? ? ? "\" did not have an index mapping. Should be one off "+
    ? ? ? ? ? fieldsToIdxMap.keySet()
    ? ? ? ? );
    ? ? }
    ? ? Object[] values = (Object[]) currentValue;
    ? ? Assertion.assertTrue(
    ? ? ? ? idxInt.intValue()
    ? ? ? ? "The index from field "+
    ? ? ? ? fieldName+
    ? ? ? ? " was illegal"
    ? ? );
    ? ? Object value= values[ idxInt.intValue() ];
    ? ? if ( logger.isDebugEnabled() )
    ? ? {
    ? ? ? ? logger.debug("fetched value "+value+" from field "+fieldName);
    ? ? }
    ? ? return value;
    ? }

    ? public boolean next() throws JRException
    ? {
    ? ? currentValue = iterator.hasNext() ? iterator.next() : null;
    ? ? return currentValue != null;
    ? }

    ? public List getValues()
    ? {
    ? ? return values;
    ? }
    }

    --------------------------------------------------------------------------------------------------------

    Using JasperReports with Hibernate
    This section will demonstrate a couple of ways to use the results of a Hibernate query as the datasource of a report in JasperReports.

    When the Hibernate query returns a simple collection of objects, The JRBeanCollectionDataSource class in the JasperReports library will work fine.

    List cats = session.find("from eg.Cat");

    Map parameters = new HashMap();
    parameters.put("Title", "The Cat Report");

    InputStream reportStream = this.class.getResourceAsStream("/the-cat-report.xml");
    JasperDesign jasperDesign = JasperManager.loadXmlDesign(reportStream);
    JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

    JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(cats);
    JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, ds);

    JasperManager.printReportToPdfFile(jasperPrint, "the-cat-report.pdf");
    However, when the Hibernate query returns tuples of objects (each tuple as an array, each array as an element in the returned List), things get a little tricky. Jasper needs a way to reference each object in the array by a field name. This class is a good solution, but at this time you are required to pass an array of field names matching the results of the query.

    public class HibernateQueryResultDataSource implements JRDataSource {

    private String[] fields;
    private Iterator iterator;
    private Object currentValue;

    public HibernateQueryResultDataSource(List list, String[] fields) {
    ? this.fields = fields;
    ? this.iterator = list.iterator();
    }

    public Object getFieldValue(JRField field) throws JRException {
    ? Object value = null;
    ? int index = getFieldIndex(field.getName());
    ? if (index > -1) {
    ? ? Object[] values = (Object[])currentValue;
    ? ? value = values[index];
    ? }
    ? return value;
    }

    public boolean next() throws JRException {
    ? currentValue = iterator.hasNext() ? iterator.next() : null;
    ? return (currentValue != null);
    }

    private int getFieldIndex(String field) {
    ? int index = -1;
    ? for (int i = 0; i < fields.length; i++) {
    ? ? if (fields
    .equals(field)) {
    ? ? index = i;
    ? ? break;
    ? ? }
    ? }
    ? return index;
    }

    }
    Now running your report would look something like this:

    List cats = session.find("select cat.type, cat.birthdate, cat.name from eg.DomesticCat cat");

    Map parameters = new HashMap();
    parameters.put("Title", "The Cat Report");

    InputStream reportStream = this.class.getResourceAsStream("/the-cat-report.xml");
    JasperDesign jasperDesign = JasperManager.loadXmlDesign(reportStream);
    JasperReport jasperReport = JasperManager.compileReport(jasperDesign);

    String[] fields = new String[] { "type", "birthdate", "name"};
    HibernateQueryResultDataSource ds = new HibernateQueryResultDataSource(cats, fields);
    JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, ds);

    JasperManager.printReportToPdfFile(jasperPrint, "the-cat-report.pdf");

    --------------------------------------------------------------------------------

    Another, alternate implementation, from Erik Romson:

    public class HibernateQueryResultDataSource implements JRDataSource
    {
    ? private static final transient Logger logger =
    ? ? Logger.Factory.getInstance(HibernateQueryResultDataSource.class);
    ? protected HashMap fieldsToIdxMap=new HashMap();
    ? protected Iterator iterator;
    ? protected Object currentValue;
    ? List values;

    ? public HibernateQueryResultDataSource(List list, String query)
    ? {
    ? ? int start =query.indexOf("select ");
    ? ? int stop =query.indexOf(" from ");
    ? ? Assertion.assertTrue(
    ? ? ? (start!=-1) &&
    ? ? ? (stop!=-1),
    ? ? ? "The query "+
    ? ? ? query+
    ? ? ? " must be of the form select x,x from ..."
    ? ? );
    ? ? start+="select".length();
    ? ? String parameters=query.substring(start,stop);
    ? ? parameters=parameters.trim();
    ? ? Assertion.assertTrue(
    ? ? ? ? parameters.length()>0,
    ? ? ? ? "The query "+
    ? ? ? ? query+
    ? ? ? ? " seems to be weird"
    ? ? );
    ? ? StringTokenizer tokenizer=new StringTokenizer(parameters,",");
    ? ? int idx=0;
    ? ? while( tokenizer.hasMoreTokens() )
    ? ? {
    ? ? ? ? String parameter=tokenizer.nextToken();
    ? ? ? ? fieldsToIdxMap.put( parameter.trim(), new Integer(idx) );
    ? ? ? ? idx++;
    ? ? }
    ? ? values=list;
    ? ? this.iterator = list.iterator();
    ? }

    ? public Object getFieldValue(JRField field) throws JRException
    ? {
    ? ? String fieldName=field.getName().replace('_','.');
    ? ? //JasperReports does not allow '.' in field names, so we
    ? ? //use '_' for nested properties instead and must convert here
    ? ? //(comment added by kbrown)
    ? ? Integer idxInt=(Integer) fieldsToIdxMap.get(fieldName);
    ? ? if (idxInt==null)
    ? ? {
    ? ? ? ? Assertion.fail(
    ? ? ? ? ? "The field \""+
    ? ? ? ? ? fieldName+
    ? ? ? ? ? "\" did not have an index mapping. Should be one off "+
    ? ? ? ? ? fieldsToIdxMap.keySet()
    ? ? ? ? );
    ? ? }
    ? ? Object[] values = (Object[]) currentValue;
    ? ? Assertion.assertTrue(
    ? ? ? ? idxInt.intValue()
    ? ? ? ? "The index from field "+
    ? ? ? ? fieldName+
    ? ? ? ? " was illegal"
    ? ? );
    ? ? Object value= values[ idxInt.intValue() ];
    ? ? if ( logger.isDebugEnabled() )
    ? ? {
    ? ? ? ? logger.debug("fetched value "+value+" from field "+fieldName);
    ? ? }
    ? ? return value;
    ? }

    ? public boolean next() throws JRException
    ? {
    ? ? currentValue = iterator.hasNext() ? iterator.next() : null;
    ? ? return currentValue != null;
    ? }

    ? public List getValues()
    ? {
    ? ? return values;
    ? }
    }

    --------------------------------------------------------------------------------

    Using the information from this page I've made a patch to JasperReports 0.6.5. You can find the patch at:
    http://www.waltermourao.com.br/JasperReports/jr65_hql.zip or you can download the full version from: http://www.waltermourao.com.br/JasperReports/jasperreports-hql-0.6.5.zip

    posted on 2006-06-29 19:58 matthew 閱讀(1612) 評論(0)  編輯  收藏 所屬分類: 報表設(shè)計與開發(fā)
    主站蜘蛛池模板: 国产免费av片在线播放| 亚洲精品中文字幕麻豆| 成人免费午夜在线观看| 两个人看的www视频免费完整版| 亚洲一区二区三区在线| 亚洲国产精品无码久久SM| 免费精品国产自产拍观看| 18禁止观看免费私人影院| 久久国产乱子伦精品免费强| 免费VA在线观看无码| 亚洲色偷偷综合亚洲av78| 亚洲白色白色在线播放| 亚洲国产成人久久精品影视| 亚洲色欲色欲www在线丝| 免费a级毛片大学生免费观看 | 中文字幕亚洲激情| 国产又长又粗又爽免费视频| 巨胸喷奶水视频www网免费| 69式互添免费视频| 96免费精品视频在线观看| 日本在线免费观看| 3344在线看片免费| 中文字幕a∨在线乱码免费看| 水蜜桃视频在线观看免费| 国产成人精品亚洲一区| 久久精品国产亚洲AV未满十八| 亚洲欧洲国产综合AV无码久久| 亚洲人成77777在线观看网| 亚洲精品中文字幕无乱码麻豆| 亚洲黄色在线播放| 亚洲视频一区二区三区| 亚洲AV午夜成人影院老师机影院| 亚洲香蕉网久久综合影视| 亚洲色欲一区二区三区在线观看 | 国产日韩久久免费影院| jizz中国免费| 国产乱子伦精品免费视频| 中文字幕免费视频精品一| 你是我的城池营垒免费看 | 久久国产亚洲精品无码| 久久精品国产亚洲精品2020|