-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Hibernate + JasperReports + iReports
PostPosted: Mon Oct 17, 2005 2:24 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Hibernate version: 3.0
Name and version of the database you are using: Firebird 1.5

Hello everyone,
I'm not sure, but I hope this is the right place to discuss this kind of doubt, 'cause I suppose I'll find here people who have already worked with these tools.

I'm a newbie to the combination just said above. I have started reading about it, and I've through the following material:

http://www.hibernate.org/79.html
http://www.jroller.com/page/wakaleo/?an ... perreports
http://sourceforge.net/docman/?group_id=64348

My doubt is:
- When using hibernate with iReports, what kind of datasource should I use? Javabeans set datasource, custom jrdatasource, or jrdatasource provider? I suppose it's number 2, right? I'm kinda lost here because I could easily create a JDBC datasource and test it, but still have had no success in creating one for use with hibernate. I can't create a report using a JDBC datasource and then use it in my system with JRBeanCollectionDataSource, can I? How should I to to create one for this use?

I suppose someone here has already used iReports to create xml files for using with Hibernate, so I'm asking for help.

If you have any material that could be useful, please, show me 'cause I'd like to read it.

Thank you all so much.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 2:38 pm 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
I am still fairly new to Hibernate though I know Jasper and iReport well. If you are using EJBs you could go with JRBeanCollectionDataSource using vectors etc... but it sounds like you need to the the JRDataSource. Use your JRDataSource to interact with your POJOs.

I would stay away from trying to make jasper connect right to the db or run sql from inside of it. The datasource is a cleaner way to go. but that is just my opinion.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 2:49 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Thanks very much,

I'm not using EJBs, just ordinary POJOs. So JRDataSource is the way to go, I guess? So this http://www.hibernate.org/79.html is not useful for me, 'cause it uses JRBeanCollectionDataSource? So I must create a JRDataSource to get the data from Hibernate? I'm not really sure about how this works, so if you could give me a little more details I'd thank you very much.

As for the SQL advice, thanks very much too. I won't do it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 3:44 pm 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
There are 3 examples T http://www.hibernate.org/79.html
1. The collection works if you are returning a list or set
2 - 3. The second one uses the datasource I was talking about but a little different then I was referring to.

The easiest way IMO is to set up a DS for the report or multiple if you will be using subreports, and use the getFieldValue(JRField field) which your datasource must expose to return your results. I am not sure what you are looking for. There are many way to acomplish what this. Tell me more about what you are trying to do and where you are having a hard time and I will try to help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 6:28 am 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Actually I'm not looking for anything fancy. Just wanna make it work and don't know exactly how. Please evaluate my idea of how it works, and correct me if I'm wrong.

1. I have the implementation of the JRDataSource, which has the getFieldValue method. I got the second implementation from the hibernate site:
Code:
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[i].equals(field)) {
        index = i;
        break;
      }
    }
    return index;
  }
}


Isn't this the correct one? What should be different?

2. On iReports, I configure my datasource using custom jrdatasource. Then this class will be my factory class. But what will be my method to retrieve the JRDataSource?

3. Then, with these things ready, how do I do my statements? I can't use SQL query, it won't accept HQL. I suppose I don't do nothing, just send the data ready to the report. But how do I refer to it there?

Really, I know there are basic things here, but my problem would probably be solved with an example or a more specific tutorial.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 7:58 am 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
OK I think you are missing the point of using the datasource. If you use the DS you don't run queires within your jrxml/jasper file. You construct the JasperPrint with your datasource instead of a connection. You used example 2 from the link but look at the putting together of the report they provide
Code:
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");


They have they results in cats and the fields in fields which they use to construct their datasource. Think of the jrxml/jasper file which you can use iReport to make as a template. You aren't putting any real data logic in it. Only a way to talk to your datasource which was passed in the constructor of JasperPrint. In this example you would have 3 fields to create in your jrxml {type, birthdate, name} then use those fields in your report probally in the detail band so it displays the record for each result.


Top
 Profile  
 
 Post subject: Error in the above Code
PostPosted: Wed Dec 21, 2005 10:36 am 
Newbie

Joined: Wed Aug 24, 2005 12:07 pm
Posts: 17
Hi All,
I am using the same code which is mentioned above but it is giving me some error.The code is
Code:
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[i].equals(field)) {
        index = i;
        break;
      }
    }
    return index;
  }
}


In this code it is giving me ClassCasteException at line
Object[] values = (Object[])currentValue;

This code runs fine if I hard code the object as follows

Project project = (Project)currentValue;
value = project.getProjectName();

I have been trying hard to solve this error but till now I didn't got any success.All suggestions are welcome.
Thanx in Advance
Chirag.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.