JasperReport与JPA结合设计报表

最近思考采用JasperReport设计报表,用JPA技术填充报表后存放在服务端会话中(session),采用jasperReport XMLServlet分页返回XML给flex客户端。结合jasperReport flash viewer组件在客户端预览和打印报表。

前一博文中的案例是采用JDBC返回数据集给JasperReport,今天看到JasperReport的数据源可以是JavaBean Collection。于是想采用DAO返回数据集后给JRBeanDataSource,加上JPA的延迟加载技术就可以呈现报表。

一、JPA模型如下:

 

org.im.scm.domain.Material.java
  1. package org.im.scm.domain;  
  2.   
  3. import …  
  4.   
  5.   
  6. /** 
  7.  * Material entity. 
  8.  *  
  9.  * @author MyEclipse Persistence Tools 
  10.  */  
  11. //@Proxy(lazy = false)  
  12. @Entity  
  13. @Table(name = "Material", schema = "dbo", catalog = "imc_manager")  
  14. public class Material implements java.io.Serializable {  
  15.   
  16.     // Fields  
  17.   
  18.     private Integer autoid;  
  19.     private MaterialType materialType;  
  20.   
  21.     // Constructors  
  22.   
  23.     /** default constructor */  
  24.     public Material() {  
  25.     }  
  26.   
  27.     /** minimal constructor */  
  28.     public Material(Integer autoid) {  
  29.         this.autoid = autoid;  
  30.     }  
  31.   
  32.     /** full constructor */  
  33.     public Material(Integer autoid, MaterialType materialType) {  
  34.         this.autoid = autoid;  
  35.         this.materialType = materialType;  
  36.     }  
  37.   
  38.     // Property accessors  
  39.     @Id  
  40.     @Column(name = "autoid", unique = true, nullable = false)  
  41.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  42.     public Integer getAutoid() {  
  43.         return this.autoid;  
  44.     }  
  45.   
  46.     public void setAutoid(Integer autoid) {  
  47.         this.autoid = autoid;  
  48.     }  
  49.       
  50.     @ManyToOne(fetch=FetchType.LAZY)  
  51.     @JoinColumn(name = "materialTypeId")  
  52.     public MaterialType getMaterialType() {  
  53.         return this.materialType;  
  54.     }  
  55.   
  56.     public void setMaterialType(MaterialType materialType) {  
  57.         this.materialType = materialType;  
  58.     }  
  59.   
  60. }  

 

Java代码:org.im.scm.domain.MaterialType
  1. package org.im.scm.domain;  
  2.   
  3. import …  
  4.   
  5. /** 
  6.  * MaterialType entity. 
  7.  *  
  8.  * @author MyEclipse Persistence Tools 
  9.  */  
  10. @Entity  
  11. @Table(name = "materialType", schema = "dbo", catalog = "imc_manager")  
  12. public class MaterialType implements java.io.Serializable {  
  13.   
  14.     // Fields  
  15.   
  16.     private Integer autoId;  
  17.     private String name;  
  18.     private String type;  
  19.     private Set<Material> materials = new HashSet<Material>(0);  
  20.   
  21.     // Constructors  
  22.   
  23.     /** default constructor */  
  24.     public MaterialType() {  
  25.     }  
  26.   
  27.     /** minimal constructor */  
  28.     public MaterialType(Integer autoId, String type) {  
  29.         this.autoId = autoId;  
  30.         this.type = type;  
  31.     }  
  32.   
  33.     /** full constructor */  
  34.     public MaterialType(Integer autoId, String name, String type,  
  35.             Set<Material> materials) {  
  36.         this.autoId = autoId;  
  37.         this.name = name;  
  38.         this.type = type;  
  39.         this.materials = materials;  
  40.     }  
  41.   
  42.     // Property accessors  
  43.     @Id  
  44.     @Column(name = "autoId", unique = true, nullable = false)  
  45.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  46.     public Integer getAutoId() {  
  47.         return this.autoId;  
  48.     }  
  49.   
  50.     public void setAutoId(Integer autoId) {  
  51.         this.autoId = autoId;  
  52.     }  
  53.   
  54.     @Column(name = "name", length = 40)  
  55.     public String getName() {  
  56.         return this.name;  
  57.     }  
  58.   
  59.     public void setName(String name) {  
  60.         this.name = name;  
  61.     }  
  62.   
  63.     @Column(name = "type", nullable = false, length = 2)  
  64.     public String getType() {  
  65.         return this.type;  
  66.     }  
  67.   
  68.     public void setType(String type) {  
  69.         this.type = type;  
  70.     }  
  71.   
  72.     @OneToMany(cascade = CascadeType.ALL, mappedBy = "materialType", fetch = FetchType.LAZY)  
  73.     @OrderBy  
  74.     public Set<Material> getMaterials() {  
  75.         return this.materials;  
  76.     }  
  77.   
  78.     public void setMaterials(Set<Material> materials) {  
  79.         this.materials = materials;  
  80.     }  
  81.   
  82. }  

二、在应用服务中调用org.im.scm.domain.MaterialDAO.findAll();

 

Java代码
  1. private JRDataSource getJRDataSource(){  
  2.         JRBeanCollectionDataSource s= new JRBeanCollectionDataSource(materialDao.findAll());   
  3.         return s;  
  4.     }  

三、报表设置如下图:

建立字段autoId 类型为java.lang.Integer,字段materialType 类型为org.im.scm.domain.MaterialType;

在report属性"import"中导入"org.im.scm.domain.*",不然在服务中无法编译为Jasper文件,系统也不会提示出错,这个我也搞不明白。

四、服务器运行结果如下