配置Model类以使Object to Xml coversion生成外键的详细信息

时间:2015-02-05 09:33:08

标签: java javascript xml json jaxb

我有两个班级名为雇主和雇员。

Employer.Java

@XmlRootElement(name = "Employer")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "employer")
public class Employer implements Serializable{
    @Id
    @GeneratedValue
    @Column(name = "id", length = 10)
    private int id;

    @XmlElement(name = "companyName")
    @Column(name = "name", length = 50)
    private String companyName;

    // Getter and setter methods
}

Employee.Java

@XmlRootElement(name = "Employee")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "employee")
public class Employee implements Serializable{
    @Id 
    @GeneratedValue
    @Column(name = "id", length=10)
    private int id;

    @XmlElement(name = "name")
    @Column(name = "name", length=50)
    private String name;

    @XmlElement(name = "email")
    @Column(name = "email", length=50)
    private String email;

    // What should I put here If I want the details of Employer class also to be converted into xml data while Object to XML conversion using JAXB
    @ManyToOne(cascade={CascadeType.PERSIST},fetch = FetchType.LAZY)
    @JoinColumn(name="employer_id")
    private Employer employer;

    // Getter and setter methods
}

通常我会通过获取数据列表然后将其转换为Json来实现,然后我用javascript解析JSon并使用_.each我将满足我的需要。如果我想得到雇主的名字,我将简单地称之为row.employer.name(Underscore JS)来使用该名称。但由于它是一个原始的xml数据,我不知道该怎么做。如果我可以学习如何在xml中生成嵌套对象数据,那么我可以继续,否则我可以通过手动创建Object到xml来手动完成。

0 个答案:

没有答案