Registration of JavaBean Fields

One of the most advanced features of JasperReports is the ability to manage data sources that are not 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 that you are using objects of this Java class:


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 that 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.
4. 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 in order to retrieve the value for the specified field.

Jaspersoft Studio parses a description such as address.state (with a period character between the two attributes) as an attribute path. This attribute path is passed to the function getAddress() in order to locate the target attribute, and then to getState() in order to query the status of the attribute. Paths may be arbitrary 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.