0

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.

Nilesh
  • 624
  • 1
  • 7
  • 23
  • How are you using this? What is the exact error you are getting? If the error has a LINE NUMBER, indicate in your code which line is that. – Alexandre Santos Jul 20 '14 at 18:07
  • That's the whole problem! It doesn't show a line number or a exception stack trace. It just throws 500. When accessed from curl or a browser. I set curl accept header properly. – Nilesh Jul 21 '14 at 01:19
  • There is a stack trace. Maybe logging is not enabled. Maybe you are not looking in the right folder, but believe me, there is a stack trace somewhere. – Alexandre Santos Jul 21 '14 at 03:16
  • A similar one found here http://stackoverflow.com/questions/18210680/glassfish-4-0-w-jersey-returns-500-internal-server-error-without-exception The solution described there doesn't apply to me because I don't have those at the first place. This seems to be something different. I even tried catching exception on em.find() and log using Logger, but nothing caught. – Nilesh Jul 21 '14 at 15:31
  • See [my answer](http://stackoverflow.com/a/40897559/1751640) – Mordechai Nov 30 '16 at 20:25

1 Answers1

0

Solution found at http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html

The exception can be seen if a string is returned instead of the POJO object and the marshaling is done manually.

Nilesh
  • 624
  • 1
  • 7
  • 23