Child pages
  • adding a to-many relationship
Skip to end of metadata
Go to start of metadata

Image you have an entity Project with a to-many relationship to ProjectStep. I wanted to present a report with rows for the projects and a sub report on each row listing the steps.

My solution was to create a getter in my Project EO:

public JRBeanCollectionDataSource theSteps() {

    return new JRBeanCollectionDataSource( ERXArrayUtilities.sortedArraySortedWithKey(this.projectSteps(), "dueBy") );

}

of course I had to import into my Project EO:

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

now I can access my relation 'theSteps' in iReport as a Field. I can then pass the field to my sub report. The only downside was that if I had a attribute 'stepDescription' in my relationship, iReport was expecting there to be a getter getStepDescription.

so I added to my EO ProjectStep:

public String getStepDescription () {

return this.stepDescription();

}

and everyone was happy. I don't know if there is a way to get around the getter requirement.

There is a way around adding the accessors in the model. Assuming you have an entity called Person with a firstName and lastName attribute, you can create a source file called PersonBeanInfo.java with this content:

This means you can create and access fields in your Jasper .jrxml  firstName and lastName, you do not need to create the getFirstName and getLastName accessors!

 

 
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import java.util.ArrayList;
import java.util.List;
public class PersonBeanInfo extends SimpleBeanInfo {

 @Override
 public PropertyDescriptor[] getPropertyDescriptors() {

 try {
 List<PropertyDescriptor> list = new ArrayList<>();
list.add( new PropertyDescriptor( "firstName", Person.class, "firstName", "setFirstName" ) );
list.add( new PropertyDescriptor( "lastName", Person.class, "lastName", "setLastName" ) );
PropertyDescriptor[] array = new PropertyDescriptor[list.size()];
 array = list.toArray( array );
 return array;
 }
 catch( IntrospectionException e ) {
 throw new RuntimeException( e );
 }
 }
}

 

 

 

  • No labels