Thursday, 5 February 2015

Configuring a Model class to make Object to Xml coversion produces details of Foreign key



I've two classes names Employer and Employee.


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
}


Normally I would do it by fetching the list of data, then converting it into Json, then I parse the JSon back in javascript and using _.each I'll do my appropriate needs. In that if I want get the name of employer, I'll simple call it as row.employer.name (Underscore JS) to use the name. But Since it's a raw xml data here, I dont have any clue on what to do. If I can learn on how to produce nested objects data in xml, then I can proceed on, otherwise I've to do it manually by creating Object to xml manually.


No comments:

Post a Comment