在Spring-JPA应用程序中使用JAXB访问对象

时间:2014-01-27 10:26:30

标签: java spring jpa jaxb

请使用JAXB注释帮助我在下面的代码中访问Employee对象。该应用程序是在JPA SPRING中开发的。我们无法访问子对象属性,即员工属性

资源核心文件

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name="resource")
@Entity
@Table(name = "resource")
public class Resource implements java.io.Serializable {


    private Integer resourceId;
    private String resourceCode;
    private String resourceName;
    private String resourceNumber;
    private Employee employee;


    public Resource() {
    }


    public Resource(Employee employee,String resourceCode, String resourceName,
            String resourceNumber
            ) {
        this.employee = employee;
        this.resourceCode = resourceCode;
        this.resourceName = resourceName;
        this.resourceNumber = resourceNumber;
    }


    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "resource_id", unique = true, nullable = false)
    public Integer getResourceId() {
        return this.resourceId;
    }

    public void setResourceId(Integer resourceId) {
        this.resourceId = resourceId;
    }

    @Column(name = "resource_code")
    public String getResourceCode() {
        return this.resourceCode;
    }

    public void setResourceCode(String resourceCode) {
        this.resourceCode = resourceCode;
    }

    @Column(name = "resource_number")
    public String getResourceNumber() {
        return this.resourceNumber;
    }

    public void setResourceNumber(String resourceNumber) {
        this.resourceNumber = resourceNumber;
    }

    @Column(name = "resource_name")
    public String getResourceName() {
        return this.resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id")
    public Employee getEmployee() {
        return this.employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

}

员工核心文件

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name="employee")
@Entity
@Table(name = "employee")
public class Employee implements java.io.Serializable {


    private Integer employeeId;
    private String employeeCode;
    private String employeeName;
    private List<Resource> resources = new ArrayList<Resource>(0);



    public Employee() {
    }



    public Employee(String employeeCode, String employeeName,List<Resource> resources

            ) {
        this.employeeCode = employeeCode;
        this.employeeName = employeeName;
        this.resources = resources;
    }


    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "employee_id", unique = true, nullable = false)
    public Integer getEmployeeId() {
        return this.employeeId;
    }

    public void setEmployeeId(Integer employeeId) {
        this.employeeId = employeeId;
    }

    @Column(name = "employee_code")
    public String getEmployeeCode() {
        return this.employeeCode;
    }

    public void setEmployeeCode(String employeeCode) {
        this.employeeCode = employeeCode;
    }


    @Column(name = "employee_name")
    public String getEmployeeName() {
        return this.employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "employee")
    public List<Resource> getResources() {
        return this.resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }


}

1 个答案:

答案 0 :(得分:0)

您必须在getEmployee()方法的RESOURCE CORE FILE中使用FetchType:Eager。延迟提取类型仅提取父对象。渴望拉动两者。