JAXB无法编组类型" java.lang.Class"

时间:2014-08-06 15:33:58

标签: java xml jaxb jaxb2

我想写一个XML文件,我有一个包含映射对象的实体类。 我添加了注释。现在我在JSF页面中有一个生成XML文件的按钮,但是在输出文件中我发现只有主类没有其他映射类,我有一个注释。 这是我的mainClass:

包esprit.olap.domain;

@XmlRootElement(name="Schema")
@XmlType(propOrder = { "parameters", "cubes", "dimensions", "catalogBase" })
@Entity
public class SchemaBase implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id_schema;  

private String name_schema;

private String description_schema;

private String measuresCaption_schema;

private String defaultRole_schema;


@ManyToOne(fetch=FetchType.LAZY)
private CatalogBase catalogBase;




@OneToMany (cascade={CascadeType.MERGE,CascadeType.ALL})
private List<CubeBase>cubes ;

@OneToMany (cascade={CascadeType.MERGE,CascadeType.ALL})
private List<ParameterBase>parameters;

@ManyToMany
private List<DimensionBase> dimensions;



public List<ParameterBase> getParameters() {
    return parameters;
}
@XmlElement(name="Parameter")
public void setParameters(List<ParameterBase> parameters) {
    this.parameters = parameters;
}
public List<DimensionBase> getDimensions() {
    return dimensions;
}
@XmlElement(name="Dimension")
public void setDimensions(List<DimensionBase> dimensions) {
    this.dimensions = dimensions;
}

public List<CubeBase> getCubes() {
    return cubes;
}
@XmlElement(name="Cube")
public void setCubes(List<CubeBase> cubes) {
    this.cubes = cubes;
}
public CatalogBase getCatalogBase() {
    return catalogBase;
}
public void setCatalogBase(CatalogBase catalogBase) {
    this.catalogBase = catalogBase;
}
public int getId_schema() {
    return id_schema;
}
@XmlTransient
public void setId_schema(int id_schema) {
    this.id_schema = id_schema;
}
public String getName_schema() {
    return name_schema;
}
@XmlAttribute(name="name")
public void setName_schema(String name_schema) {
    this.name_schema = name_schema;
}
public String getDescription_schema() {
    return description_schema;
}
@XmlAttribute(name="description")
public void setDescription_schema(String description_schema) {
    this.description_schema = description_schema;
}
public String getMeasuresCaption_schema() {
    return measuresCaption_schema;
}
@XmlTransient
public void setMeasuresCaption_schema(String measuresCaption_schema) {
    this.measuresCaption_schema = measuresCaption_schema;
}
public String getDefaultRole_schema() {
    return defaultRole_schema;
}
@XmlAttribute(name="defaultValue")
public void setDefaultRole_schema(String defaultRole_schema) {
    this.defaultRole_schema = defaultRole_schema;
}


public void addCubeToShema(List<CubeBase> cubes){
       for(CubeBase cubeBase:cubes)
           cubeBase.setSchema(this);
       this.cubes=cubes;
   }
public void addParameterToSchema(List<ParameterBase> parameters){
    for(ParameterBase param:parameters)
        param.setSchema(this);
    this.parameters=parameters;
}
public SchemaBase(String name_schema, String description_schema,
        String measuresCaption_schema, String defaultRole_schema,
        List<CubeBase> cubes, List<ParameterBase> parameters,
        List<DimensionBase> dimensions) {
    super();
    this.name_schema = name_schema;
    this.description_schema = description_schema;
    this.measuresCaption_schema = measuresCaption_schema;
    this.defaultRole_schema = defaultRole_schema;
    this.cubes = cubes;
    this.parameters = parameters;
    this.dimensions = dimensions;
}

public SchemaBase(String name_schema,List<CubeBase> cubes, List<ParameterBase> parameters,
        List<DimensionBase> dimensions) {
    super();
    this.name_schema = name_schema;
    this.cubes = cubes;
    this.parameters = parameters;
    this.dimensions = dimensions;
}
public SchemaBase(String name_schema) {
    super();
    this.name_schema = name_schema;
}
public SchemaBase() {
    super();
}


}

这是我的SchemaBase的ManagedBean:

@ManagedBean(name="SchemaMan")
@SessionScoped
public class SchemaManaged {

    @EJB
     SchemaDaoRemote schemaService;

    //private SchemaBase schema;
    public SchemaBase schema = new SchemaBase();
    private WriteXml xl= new WriteXml();

    private String roleSchema;
    private String descSchema;
    private String nameSchema;
    private TreeNode shemaBoxses;
    private TreeNode shemaBoxse;


    @PostConstruct
    public void init() {

     shemaBoxses = new DefaultTreeNode("Schema", null);
     TreeNode node0 = new DefaultTreeNode("Schema",  shemaBoxses);



 }

    public String getRoleSchema() {
        return roleSchema;
    }
    public void setRoleSchema(String roleSchema) {
        this.roleSchema = roleSchema;
    }
    public String getDescSchema() {
        return descSchema;
    }
    public void setDescSchema(String descSchema) {
        this.descSchema = descSchema;
    }


    public SchemaBase getSchema() {
        return schema;
    }

    public void setSchema(SchemaBase schema) {
        this.schema = schema;
    }

    public String getNameSchema() {
        return nameSchema;
    }
    public void setNameSchema(String nameSchema) {
        this.nameSchema = nameSchema;
    }
    public TreeNode getShemaBoxse() {
        return shemaBoxse;
    }
    public void setShemaBoxse(TreeNode shemaBoxse) {
        this.shemaBoxse = shemaBoxse;
    }
    public TreeNode getShemaBoxses() {
        return shemaBoxses;
    }
    public void setShemaBoxses(TreeNode shemaBoxses) {
        this.shemaBoxses = shemaBoxses;
    }

    public String CreateSchema(){
        System.out.println(schema.getName_schema());
        schema = schemaService.create_schema(schema);

        FacesContext context= FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Schema "+schema.getName_schema()+" ajouter avec succees", ""));
        return "Scucces";


}


}

以及SchemaBase的服务:

public interface SchemaDaoRemote {

    public SchemaBase create_schema (SchemaBase schema);
    public void update_schema (SchemaBase schema);
    public void delete_schema(int id);
    public List<SchemaBase>list_schema(int id);
    public SchemaBase find_schema(int id);


}

我创建了像这样的托管bean:

@ManagedBean(name = "xmlMan")
@SessionScoped
public class XmlGenaratedManaged {
    @ManagedProperty(value = "#{SchemaMan}")
    SchemaManaged shmanged;
    @ManagedProperty(value = "#{paraMan}")
    ParameterManaged param;



public SchemaManaged getShmanged() {
        return shmanged;
    }



    public void setShmanged(SchemaManaged shmanged) {
        this.shmanged = shmanged;
    }




    public ParameterManaged getParam() {
        return param;
    }



    public void setParam(ParameterManaged param) {
        this.param = param;
    }



public void GenerateXml(){
    //SchemaBase schema = new SchemaBase();
    shmanged.schema.setParameters(new ArrayList<ParameterBase>());
    //ParameterBase pb=param.getParamUpdate();
    //schema.getParameters().add(pb);

    shmanged.schema.getParameters().add(param.getParamUpdate());

        JAXBContext jaxbContext;

        try {

            jaxbContext = JAXBContext.newInstance(SchemaBase.class);

            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            System.out.println("This is your cube"+shmanged.getSchema().getParameters());
            //specify the location and name of xml file to be created
            File XMLfile = new File("C:\\EspritJSF.xml");
            // for getting nice formatted output  
               jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);


            // Writing to XML file
            jaxbMarshaller.marshal(shmanged.getSchema(), XMLfile); 


            // Writing to console
            jaxbMarshaller.marshal(shmanged.getSchema(), System.out); 
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

}

但是当我添加2例ParamterBase时,当我运行它时,我只得到了我的XML文件中的最后一个参数:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Schema defaultValue="" description="fdsd" name="fdg">
    <Parameter defaultValue="dffdfd" description="fdsf" modifiable="false" name="dfd" type="Timestamp"/>
</Schema>

那么如何在我的XML文件中提供我的两个参数,以及所有这些参数都可以帮助你。

1 个答案:

答案 0 :(得分:1)

您的类和所有设置都正确,但根据您看到的内容,您的架构对象未正确填充。 我构建了一组类来模仿你在这里用一些任意内容描述的东西,并且能够编组。

类型:

@XmlRootElement(name = "Parameter")
@XmlAccessorType(XmlAccessType.FIELD)
public class ParameterBase {
    @XmlElement(name = "Name")
    public String paramName;
    @XmlElement(name = "Value")
    public String paramValue;

    public ParameterBase() {}

    public void setParamName(String paramName) { this.paramName = paramName; }
    public void setParamValue(String paramValue) { this.paramValue = paramValue; }
    public String getParamName() { return this.paramName; }
    public String getParamValue() { return this.paramValue; }
}


@XmlRootElement(name = "Cube")
@XmlAccessorType(XmlAccessType.FIELD)
public class CubeBase {
    @XmlElement(name = "X")
    int x;
    @XmlElement(name = "Y")
    int y;
    @XmlElement(name = "Z")
    int z;

    public CubeBase() {
        x = 0;
        y = 0;
        z = 0;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public int getZ() {
        return this.z;
    }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
    public void setZ(int z) { this.z = z; }
} 



@XmlRootElement(name = "Dimension")
@XmlAccessorType(XmlAccessType.FIELD)
public class DimensionBase {
    @XmlElement(name = "Value")
    int dim;

    public DimensionBase() {
        dim = 0;
    }

    public void setDim(int dim) { this.dim = dim; }
    public int getDim() { return this.dim; }
}

@XmlRootElement(name = "Catalog")
@XmlAccessorType(XmlAccessType.FIELD)
public class CatalogBase {
    @XmlElement(name = "Name")
    String catalogName;

    public CatalogBase() {

    }

    public void setCatalogName(String name) {
        this.catalogName = name;
    }

    public String getCatalogName() {
        return this.catalogName;
    }
}

架构类型:

@XmlRootElement(name="Schema")
@XmlType(propOrder = { "parameters", "cubes", "dimensions", "catalogBase" })
@XmlAccessorType(XmlAccessType.FIELD)
public class SchemaBase {
    @XmlElement(name = "Parameter")
    List<ParameterBase> parameters;
    @XmlElement(name = "Cube")
    List<CubeBase> cubes;
    @XmlElement(name = "Dimension")
    List<DimensionBase> dimensions;
    @XmlElement(name = "CatalogBase")
    CatalogBase catalogBase;

    @XmlAttribute(name = "defaultValue")
    String defaultValue;
    @XmlAttribute(name = "description")
    String description;
    @XmlAttribute(name = "name")
    String name;

    public SchemaBase() {
        defaultValue = "dsf";
        description = "sdfsd";
        name = "dsq";
    }

    public SchemaBase(String defaultValue, String description, String name) {
        this.defaultValue = defaultValue;
        this.description = description;
        this.name = name;
    }

    public List<ParameterBase> getParameters() { return this.parameters; }
    public List<CubeBase> getCubes() { return this.cubes; }
    public List<DimensionBase> getDimensions() { return this.dimensions; }
    public CatalogBase getCatalogBase() { return this.catalogBase; }

    public void setParameters(List<ParameterBase> parameters ) { this.parameters = parameters;}
    public void setCubes(List<CubeBase> cubes ) { this.cubes = cubes;}
    public void setDimensions(List<DimensionBase> dimensions ) { this.dimensions = dimensions;}
    public void setCatalogBase(CatalogBase catalogBase) { this.catalogBase = catalogBase; }
}

通过以下简单测试:

public class Test {
    public static void main(String[] args) throws Exception {
        SchemaBase schema = new SchemaBase("Default", "MySchema", "MySchemaName");

        CatalogBase cb = new CatalogBase();
        cb.setCatalogName("My Catalog");
        schema.setCatalogBase(cb);

        schema.setDimensions(new ArrayList<DimensionBase>());
        DimensionBase db = new DimensionBase();
        db.setDim(12);
        schema.getDimensions().add(db);
        db = new DimensionBase();
        db.setDim(14);
        schema.getDimensions().add(db);
        db = new DimensionBase();
        db.setDim(50);
        schema.getDimensions().add(db);

        schema.setCubes(new ArrayList<CubeBase>());
        CubeBase cube = new CubeBase();
        cube.setX(3);
        cube.setY(13);
        cube.setZ(20);
        schema.getCubes().add(cube);

        schema.setParameters(new ArrayList<ParameterBase>());
        ParameterBase pb = new ParameterBase();
        pb.setParamName("MyParamName");
        pb.setParamValue("MyParamValue");
        schema.getParameters().add(pb);

        JAXBContext context = JAXBContext.newInstance(new Class[]{schema.getClass()});
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(schema, System.out);
    }
}

产生以下输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Schema defaultValue="Default" description="MySchema" name="MySchemaName">
    <Parameter>
        <Name>MyParamName</Name>
        <Value>MyParamValue</Value>
    </Parameter>
    <Cube>
        <X>3</X>
        <Y>13</Y>
        <Z>20</Z>
    </Cube>
    <Dimension>
        <Value>12</Value>
    </Dimension>
    <Dimension>
        <Value>14</Value>
    </Dimension>
    <Dimension>
        <Value>50</Value>
    </Dimension>
    <CatalogBase>
        <Name>My Catalog</Name>
    </CatalogBase>
</Schema>

如果创建schema且未填充任何其他内容,则会观察到以下内容:

public class Test {
    public static void main(String[] args) throws Exception {
        SchemaBase schema = new SchemaBase("Default", "MySchema", "MySchemaName");

        JAXBContext context = JAXBContext.newInstance(new Class[]{schema.getClass()});
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(schema, System.out);
    }
}

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Schema defaultValue="Default" description="MySchema" name="MySchemaName"/>

检查您的架构对象并确保它已完全填充。