Registration of JavaBean Fields

One of the most advanced features of JasperReports is its ability to manage data sources that aren't based on simple SQL queries. One example of this is JavaBean collections. In a JavaBean collection, each item in the collection represents a record. JasperReports assumes that all objects in the collection are instances of the same Java class. In this case the “fields” are the object attributes (or even attributes of attributes).

By selecting the Java Bean tab in the query designer, you can register the fields that correspond to the specified Java classes. We assume you know the Java classes that correspond to the objects that you use in your report.

JavaBeans Tab

Suppose you're using objects of this Java class:

com.jaspersoft.ireport.examples.beans.PersonBean 

To register fields for the class:

1. Put the class name in the name field and click Read attributes. Jaspersoft Studio scans the class.
2. Check the scan results to make sure Jaspersoft Studio has captured the correct object attributes for the class type.
3. Select the fields you want to use in your report and click Add.

Jaspersoft Studio creates new fields corresponding to the selected attributes and adhesion to the list. The description, in this case, stores the method that the data source must invoke to retrieve the value for the specified field.

Jaspersoft Studio parses a description such as address.state (with a period between the two attributes) as an attribute path. This attribute path is passed to the function getAddress() to locate the target attribute, and then to getState() to query the status of the attribute. Paths may be arbitrary and long, and Jaspersoft Studio can recursively parse attribute trees within complex JavaBeans and in order to register very specific fields.

We have just discussed the two tools used most frequently to register fields, but we’re not done yet. There are many other tools you can use to discover and register fields, for instance, the HQL and XML node mapping tools.