我正在学习使用JAXB和JPA开发REST应用程序。我陷入了奇怪的问题,我不知道要搜索什么。
我有这两个类:
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;
}
}
和
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;
}
}
当我在数据库中输入患者时,该服务能够将实体映射到XML / JSON。但是当我添加一个关联的处方条目时,Glassfish会抛出错误500.
可能是什么问题?
我正在使用GlassFish 4.0和EclipseLink 2.5.1。 IDE是Eclipse Luna。
答案 0 :(得分:0)
在http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
找到解决方案如果返回一个字符串而不是POJO对象并且手动完成编组,则可以看到异常。