I'm just learning to develop a REST application using JAXB and JPA. I'm stuck in weird problem and I have no clue what to search for.
I have these two classes:
package clinic.model;
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
* The persistent class for the patients database table.
*
*/
@Entity
@Table(name="patients")
@NamedQuery(name="Patient.findAll", query="SELECT p FROM Patient p")
@XmlRootElement
public class Patient implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String address;
private String area;
@Temporal(TemporalType.DATE)
private Date dob;
private BigDecimal mobile;
private String name;
private String sex;
//bi-directional many-to-one association to Prescription
@OneToMany(mappedBy="patient")
private List<Prescription> prescriptions;
public Patient() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getArea() {
return this.area;
}
public void setArea(String area) {
this.area = area;
}
public Date getDob() {
return this.dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public BigDecimal getMobile() {
return this.mobile;
}
public void setMobile(BigDecimal mobile) {
this.mobile = mobile;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@XmlList
public List<Prescription> getPrescriptions() {
return this.prescriptions;
}
public void setPrescriptions(List<Prescription> prescriptions) {
this.prescriptions = prescriptions;
}
public Prescription addPrescription(Prescription prescription) {
getPrescriptions().add(prescription);
prescription.setPatient(this);
return prescription;
}
public Prescription removePrescription(Prescription prescription) {
getPrescriptions().remove(prescription);
prescription.setPatient(null);
return prescription;
}
}
And
package clinic.model;
import java.io.Serializable;
import javax.persistence.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
* The persistent class for the prescriptions database table.
*
*/
@Entity
@Table(name="prescriptions")
@NamedQuery(name="Prescription.findAll", query="SELECT p FROM Prescription p")
public class Prescription implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Temporal(TemporalType.DATE)
private Date date;
private BigDecimal fee;
@Temporal(TemporalType.DATE)
private Date followup;
private String treatment;
//bi-directional many-to-one association to Patient
@ManyToOne(fetch=FetchType.LAZY)
private Patient patient;
//bi-directional many-to-many association to Diagnosis
@ManyToMany
@JoinTable(
name="prescriptions_diagnoses"
, joinColumns={
@JoinColumn(name="pid")
}
, inverseJoinColumns={
@JoinColumn(name="did")
}
)
private List<Diagnosis> diagnoses;
//bi-directional many-to-one association to PrescriptionDrug
@OneToMany(mappedBy="prescription")
private List<PrescriptionDrug> prescriptionsDrugs;
//bi-directional many-to-many association to Vaccine
@ManyToMany(mappedBy="prescriptions")
private List<Vaccine> vaccines;
public Prescription() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
public BigDecimal getFee() {
return this.fee;
}
public void setFee(BigDecimal fee) {
this.fee = fee;
}
public Date getFollowup() {
return this.followup;
}
public void setFollowup(Date followup) {
this.followup = followup;
}
public String getTreatment() {
return this.treatment;
}
public void setTreatment(String treatment) {
this.treatment = treatment;
}
public Patient getPatient() {
return this.patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public List<Diagnosis> getDiagnoses() {
return this.diagnoses;
}
public void setDiagnoses(List<Diagnosis> diagnoses) {
this.diagnoses = diagnoses;
}
public List<PrescriptionDrug> getPrescriptionsDrugs() {
return this.prescriptionsDrugs;
}
public void setPrescriptionsDrugs(List<PrescriptionDrug> prescriptionsDrugs) {
this.prescriptionsDrugs = prescriptionsDrugs;
}
public PrescriptionDrug addPrescriptionsDrug(PrescriptionDrug prescriptionsDrug) {
getPrescriptionsDrugs().add(prescriptionsDrug);
prescriptionsDrug.setPrescription(this);
return prescriptionsDrug;
}
public PrescriptionDrug removePrescriptionsDrug(PrescriptionDrug prescriptionsDrug) {
getPrescriptionsDrugs().remove(prescriptionsDrug);
prescriptionsDrug.setPrescription(null);
return prescriptionsDrug;
}
public List<Vaccine> getVaccines() {
return this.vaccines;
}
public void setVaccines(List<Vaccine> vaccines) {
this.vaccines = vaccines;
}
}
When I have a patient entry in the database, the service is able to map entities into XML/JSON. But when I add an associating prescription entry, Glassfish throws error 500.
What could be the problem?
I'm using GlassFish 4.0 and EclipseLink 2.5.1. IDE is Eclipse Luna.