我有主要实体和子实体。主要实体的数据持久化到derby数据库。但是子数据不能持久保存
我尝试更改XML输入仍然无法正常工作。当我在子实体上对数据进行硬编码时,硬编码的数据将保留到derby数据库中。我注意到数据到达的是父实体而不是子实体 父母:
@Entity
@XmlRootElement
public class Subject implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Float cost;
@Temporal(javax.persistence.TemporalType.DATE)
private Date reportDate;
@Temporal(javax.persistence.TemporalType.DATE)
private Date creationdate = new Date();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Qualification> qualifications;
public Subject() {
}
public Subject(Long id, String name, Float cost, Date reportDate, List<Qualification> qualifications) {
this.id = id;
this.name = name;
this.cost = cost;
this.reportDate = reportDate;
this.qualifications = qualifications;
}
public List<Qualification> getQualifications() {
return qualifications;
}
public void setQualifications(List<Qualification> qualifications) {
this.qualifications = qualifications;
}
public String getName() {
return name;
}
public Float getCost() {
return cost;
}
public Date getReportDate() {
return reportDate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreationdate() {
return creationdate;
}
public void setName(String name) {
this.name = name;
}
public void setCost(Float cost) {
this.cost = cost;
}
public void setReportDate(Date reportDate) {
this.reportDate = reportDate;
}
public void setCreationdate(Date creationdate) {
this.creationdate = creationdate;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Subject)) {
return false;
}
Subject other = (Subject) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "za.ac.tut.entity.Subject[ id=" + id + " ]";
}
public Qualification qualificationMethod(){
Qualification q =new Qualification();
q.setCode("nn");
q.setQname("tt");
return q;
}
}
孩子:
@Entity
@XmlRootElement
public class Qualification implements Serializable {
public Qualification() {
}
public Qualification(String qname, String code) {
this.qname = qname;
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getQname() {
return qname;
}
public void setQname(String qname) {
this.qname = qname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Qualification)) {
return false;
}
Qualification other = (Qualification) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "za.ac.tut.entity.Qualifications[ id=" + id + " ]";
}
}
Sevlet:
public class addSubjectServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
String qname = request.getParameter("qname");
String cost = request.getParameter("cost");
String subjectXML = createEmployeeXML(id, name,cost, qname);
InstitutionNewJerseyClient client = new InstitutionNewJerseyClient();
client.create_XML(subjectXML);
client.close();
RequestDispatcher disp = request.getRequestDispatcher("/addSubjectOutcome.jsp");
disp.forward(request, response);
log("-------------------------------------------------------------- ----------------------------------------------------------------------------------------------" );
log("employeeXMLog------- -" + subjectXML);
}
private String createEmployeeXML(String id, String name, String cost,String qname) {
String subjectXML = "<subject>"
+ "<id>" + id + "</id>"
+ "<name>" + name + "</name>"
+ "<cost>" + cost + "</cost>"
+ "<qualifications>"
+ "<qualification>"
+ "<qname>" + qname + "</qname>"
+ "<code>" + name + "</code>"
+ "</qualification>"
+ "</qualifications>"
+ "</subject>";
return subjectXML;
}
}