如何将pojo设置为moxy?

时间:2013-11-13 12:23:50

标签: java xml jaxb moxy

我有这个文件:

@XmlRootElement(name="Projects")
public class Projects extends ResponseWrapper {
    private ArrayList<Project> project;
    @XmlElement(name="Project")
    public ArrayList<Project> getProject() {
        return project;
    }

    public void setProject(List<Project> list) {
        this.project = (ArrayList<Project>)list;
    }

}

Project有一个@XMLInverseReference

  System.out.println(JAXBContext.newInstance(Projects.class).getClass()); //error

Exception Description: The method [setProject] or [getProject] is not defined in the object [com.tps.www.data.Projects].

打印出Project.class时:

 System.out.println(JAXBContext.newInstance(Project.class).getClass()); //OK
20:27:01,118 INFO  [stdout] (http-localhost-127.0.0.1-8180-1) class org.eclipse.persistence.jaxb.JAXBContext



package com.tps.www.data;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import com.tps.www.util.ResponseWrapper;

@NamedQueries({ 
@NamedQuery(name = "findProjectById", query = "select p from Project p where p.projectCode = ?"),
@NamedQuery(name="findAllProject",query="from Project p")})

@Entity
@Table(name = "Project")
@XmlRootElement(name = "Project",namespace="http://www.w3.org/2001/XMLSchema-instance")
public class Project extends ResponseWrapper implements Serializable {
    @Id
    @GeneratedValue
    @Column(name = "Project_Code")
    private String projectCode;

    @Column(name = "Project_Name")
    private String projectName;
    @Column(name = "Starting_Date")
    private Date startingDate;
    @Column(name = "Expected_End_Date")
    private Date expectedEndDate;
    @Column(name = "Created_By")
    private String createdBy;

    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToOne(mappedBy = "project",fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    private Contract contract;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "Service_Id")
    private Service service;

    @OneToOne(mappedBy = "project",fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    private BudgetRequestInfo budgetRequestInfo;
    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(mappedBy = "project",cascade=CascadeType.ALL)
    private List<MaterialRequestAssignment> materialRequestAssignment;

    @XmlElement(name = "Project_Code", nillable=true)
    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    @XmlElement(name = "Project_Name", nillable=true)
    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    @XmlElement(name = "Starting_Date", nillable=true)
    public Date getStartingDate() {
        return startingDate;
    }

    public void setStartingDate(Date startingDate) {
        this.startingDate = startingDate;
    }

    @XmlElement(name = "Expected_End_Date", nillable=true)
    public Date getExpectedEndDate() {
        return expectedEndDate;
    }

    public void setExpectedEndDate(Date expectedEndDate) {
        this.expectedEndDate = expectedEndDate;
    }

    @XmlElement(name = "Created_By", nillable=true)
    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @XmlElement(name = "Contract", nillable=true)
    @XmlInverseReference(mappedBy="project")
    public Contract getContract() {
        return contract;
    }

    public void setContract(Contract contract) {
        this.contract = contract;
    }

    @XmlElement(name = "Service_Id", nillable=true)
    @XmlInverseReference(mappedBy="project")
    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    @XmlElement(name = "Budget_Request_Info", nillable=true)
    @XmlInverseReference(mappedBy="project")
    public BudgetRequestInfo getBudgetRequestInfo() {
        return budgetRequestInfo;
    }

    public void setBudgetRequestInfo(BudgetRequestInfo budgetRequestInfo) {
        this.budgetRequestInfo = budgetRequestInfo;
    }
    @XmlElement(name = "Material_Request_Assignment", nillable=true)
    @XmlInverseReference(mappedBy="project")
    public List<MaterialRequestAssignment> getMaterialRequestAssignment() {
        return materialRequestAssignment;
    }

    public void setMaterialRequestAssignment(
            List<MaterialRequestAssignment> materialRequestAssignment) {
        this.materialRequestAssignment = materialRequestAssignment;
    }




}

1 个答案:

答案 0 :(得分:0)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-22)专家组的成员。

我无法重现您所看到的问题。你能帮忙提供更多信息,以便我能重现这个问题吗?这是我尝试过的:

  1. 删除了JPA&amp;来自Project类的Hibernate注释以使其编译。
  2. ResponseWrapperProject创建了空Projects个课程以进行扩展。
  3. Project引用的每个类创建的类。
  4. 当某个属性标有@XmlInverseReference时,会在相应类型上添加project属性:

    public class Contract {
    
        private Project project;
    
        public Project getProject() {
            return project;
        }
    
        public void setProject(Project project) {
            this.project = project;
        }
    
    }
    
  5. 请使用以下演示代码:

    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Projects.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum19953731/input.xml");
            Projects projects = (Projects) unmarshaller.unmarshal(xml);
        }
    
    }