I want to generate a JasperReports report from two tables. Tables are emp
and desig
Respected bean classes are ,
@Entity
@Table(name = "desg")
public class Designation {
@Id
@Column(name="Empid")
private int eId;
@Column(name="Designation")
private String Designation;
// getters and setters
}
@Entity
@Table(name = "emp")
public class Employee {
@Id
@GeneratedValue
@Column(name="Eid")
private int eId;
@Column(name="Name")
private String name;
@Column(name="Address")
private String address;
@Column(name="Salary")
private int salary;
// getters and setters
}
The report with only one table is working fine . I use Hibernate to create list of bean object. I request for report as follow,
List<Object[]> lst= sessio.createQuery("select e.eId, e.name, d.Designation From Employee e, Designation d where e.eId=d.eId ").list();
String reportSourceFile="E:\\classes\\report4.jrxml";
JasperReport jasperReport = null;
JasperDesign jasperDesign = null;
Map parameters = new HashMap();
parameters.put("Title", "The EMP Report");
try {
jasperDesign = JRXmlLoader.load(reportSourceFile);
jasperReport = JasperCompileManager.compileReport(jasperDesign);
byte[] byteStream = JasperRunManager.runReportToPdf(jasperReport, parameters, new JRBeanCollectionDataSource(emplst)); // what to change instead of emplst
OutputStream outStream = response.getOutputStream();
response.setHeader("Content-Disposition","filename=myReport.pdf");
response.setContentType("application/pdf");
response.setContentLength(byteStream.length);
outStream.write(byteStream,0,byteStream.length);
} catch (JRException e1) {
e1.printStackTrace();
}
If I send my emplst
it works fine but I want lst
to be print in report. How to send lst
object to JasperReports 's report and how to get in iReport tool?
In your JRXML use import tag
<import value="yourPackageName.Employee">
and Create a Parameter in JRXML having data type as java.lang.Object
lets say myEmployee
is the parameter name
then pass Employee Object
as parameter to JRXML
Map<String,Object> map=new HashMap<String,Object>();
map.put("myEmployee",yourEmployeeObject);
Now your employee class Object is present in JRXML , you can call getters from JRXML to get the State of Object ( ie attributes of Object
)
you can:
1) create wrapper class for your joined objects to pass to JRBeanCollectionDataSource
or
2) Create own implementation of JRDataSource
or
3) fill report based on sql, not datasource
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.