最近思考采用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
- package org.im.scm.domain;
- import …
- /**
- * Material entity.
- *
- * @author MyEclipse Persistence Tools
- */
- //@Proxy(lazy = false)
- @Entity
- @Table(name = "Material", schema = "dbo", catalog = "imc_manager")
- public class Material implements java.io.Serializable {
- // Fields
- private Integer autoid;
- private MaterialType materialType;
- // Constructors
- /** default constructor */
- public Material() {
- }
- /** minimal constructor */
- public Material(Integer autoid) {
- this.autoid = autoid;
- }
- /** full constructor */
- public Material(Integer autoid, MaterialType materialType) {
- this.autoid = autoid;
- this.materialType = materialType;
- }
- // Property accessors
- @Id
- @Column(name = "autoid", unique = true, nullable = false)
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- public Integer getAutoid() {
- return this.autoid;
- }
- public void setAutoid(Integer autoid) {
- this.autoid = autoid;
- }
- @ManyToOne(fetch=FetchType.LAZY)
- @JoinColumn(name = "materialTypeId")
- public MaterialType getMaterialType() {
- return this.materialType;
- }
- public void setMaterialType(MaterialType materialType) {
- this.materialType = materialType;
- }
- }
Java代码:org.im.scm.domain.MaterialType
- package org.im.scm.domain;
- import …
- /**
- * MaterialType entity.
- *
- * @author MyEclipse Persistence Tools
- */
- @Entity
- @Table(name = "materialType", schema = "dbo", catalog = "imc_manager")
- public class MaterialType implements java.io.Serializable {
- // Fields
- private Integer autoId;
- private String name;
- private String type;
- private Set<Material> materials = new HashSet<Material>(0);
- // Constructors
- /** default constructor */
- public MaterialType() {
- }
- /** minimal constructor */
- public MaterialType(Integer autoId, String type) {
- this.autoId = autoId;
- this.type = type;
- }
- /** full constructor */
- public MaterialType(Integer autoId, String name, String type,
- Set<Material> materials) {
- this.autoId = autoId;
- this.name = name;
- this.type = type;
- this.materials = materials;
- }
- // Property accessors
- @Id
- @Column(name = "autoId", unique = true, nullable = false)
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- public Integer getAutoId() {
- return this.autoId;
- }
- public void setAutoId(Integer autoId) {
- this.autoId = autoId;
- }
- @Column(name = "name", length = 40)
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Column(name = "type", nullable = false, length = 2)
- public String getType() {
- return this.type;
- }
- public void setType(String type) {
- this.type = type;
- }
- @OneToMany(cascade = CascadeType.ALL, mappedBy = "materialType", fetch = FetchType.LAZY)
- @OrderBy
- public Set<Material> getMaterials() {
- return this.materials;
- }
- public void setMaterials(Set<Material> materials) {
- this.materials = materials;
- }
- }
二、在应用服务中调用org.im.scm.domain.MaterialDAO.findAll();
Java代码
- private JRDataSource getJRDataSource(){
- JRBeanCollectionDataSource s= new JRBeanCollectionDataSource(materialDao.findAll());
- return s;
- }
三、报表设置如下图:
建立字段autoId 类型为java.lang.Integer,字段materialType 类型为org.im.scm.domain.MaterialType;

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

四、服务器运行结果如下
