无法使用外键struts2插入数据

时间:2017-05-11 01:32:39

标签: java hibernate struts2

我有3个实体(MedecinPatientRendezvous

Medecin与Idmed,患者Idpat是外键,在Rendezvous中有ManyToOne关系。

当我通过表单添加MedecinPatient时,数据会完美存储。

但是当我尝试添加Rendezvous时,使用带有两个struts2选择标记的表单,<:select../>填充了患者和Medecin的Id列表。

什么都没发生。

我返回成功,但数据未存储。

如何解决这个问题?

我的表格,Rendezvous.jsp

 <div class="col-md-4">
    <div class="form_main">
            <h4 class="heading"><strong>Planifier un </strong> Rendezvous <span></span></h4>
            <div class="form">
            <s:form action="Ajoutrdv" id="Ajoutrdv" namepace="/pages" theme="bootstrap" cssClass="bs-example form-horizontal">
                <s:textfield label="Id du Rendezvous" type="Number" min="1" required="" placeholder="Entrer l'ID du RDV"  name="idrdv"/>
                <s:select label="Id du Patient" headerKey="-1" headerValue="" list="%{Patient}" listKey="idpat" name="idpat"  required="" onchange="getData(this.value)"/>
                <s:select label="Id du Medecin" headerKey="-1" headerValue="" list="%{Medecin}" listKey="idmed" name="idmed"  required="" onchange="getData(this.value)"/>
                <s:textfield type="Date" required="" name="date"></s:textfield>
                <s:textfield type="Time" required="" name="heure"></s:textfield>
                <s:textarea label="Motif" required="" name="motif"></s:textarea>


                <caption><s:submit cssClass="btn btn-primary" value="Enregistrer"/></caption>
            </s:form>
        </div>

我在struts2.xml中的操作

<package name="Ajoutrdv" namespace="/pages" extends="struts-default">
    <action name="Ajoutrdv" class="controller.FormRDVAction" method="Ajoutrdv">
        <result name="success">/pages/accueil.jsp</result>
        <result name="input">/pages/Rendezvous.jsp</result>
    </action>
</package>

FormRDVAction

public class FormRDVAction extends ActionSupport {
private Integer idrdv;
private Medecin idmed;
private Patient idpat;
private String date;
private String heure;
private String motif;
private List<Rendezvous> Rendezvous;
// getters setters

// Add action
public String Ajoutrdv() {


     RendezvousDAO rdvDAO= RendezvousDAO.getInstance();

     Rendezvous R = new Rendezvous();
     R.setIdmed(idmed);
     R.setIdpat(idpat);
     R.setIdrdv(idrdv);
     R.setDate(date);
     R.setHeure(heure);
     R.setMotif(motif);
     rdvDAO.saveRendezvous(R);
    return SUCCESS;

}
}

RendezvousDAO

public void saveRendezvous(Rendezvous Rendezvous) {
    log.info("********** Debut saveRendezvous RendezvousDAO **********");
    Session session = SessionFactoryUtil.getInstance().openSession();
    Transaction trans = session.beginTransaction();
    try {

        session.save(Rendezvous);
          if (!trans.wasCommitted()) {
        trans.commit();
          }

    } catch (Exception e) {
        e.printStackTrace();
        log.fatal(e.getMessage());
        trans.rollback();

    } finally {
        session.close();

}      
}

最后,映射类Rendezvous.java

@Entity
@Table(name = "rendezvous")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Rendezvous.findAll", query = "SELECT r FROM Rendezvous r"),
@NamedQuery(name = "Rendezvous.findByIdrdv", query = "SELECT r FROM Rendezvous r WHERE r.idrdv = :idrdv"),
@NamedQuery(name = "Rendezvous.findByDate", query = "SELECT r FROM Rendezvous r WHERE r.date = :date"),
@NamedQuery(name = "Rendezvous.findByHeure", query = "SELECT r FROM Rendezvous r WHERE r.heure = :heure"),
@NamedQuery(name = "Rendezvous.findByMotif", query = "SELECT r FROM Rendezvous r WHERE r.motif = :motif")})
public class Rendezvous implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "idrdv")
private Integer idrdv;
@Basic(optional = false)
@Column(name = "date")
private String date;
@Basic(optional = false)
@Column(name = "heure")
private String heure;
@Basic(optional = false)
@Column(name = "motif")
private String motif;
@JoinColumn(name = "idpat", referencedColumnName = "idpat")
@ManyToOne(optional = false , fetch = FetchType.LAZY)
private Patient idpat;
@JoinColumn(name = "idmed", referencedColumnName = "idmed")
@ManyToOne(optional = false , fetch = FetchType.LAZY)
private Medecin idmed;

public Rendezvous() {
}

public Rendezvous(Integer idrdv) {
    this.idrdv = idrdv;
}

public Rendezvous(Integer idrdv, String date, String heure, String motif) {
    this.idrdv = idrdv;
    this.date = date;
    this.heure = heure;
    this.motif = motif;
}

public Integer getIdrdv() {
    return idrdv;
}

public void setIdrdv(Integer idrdv) {
    this.idrdv = idrdv;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getHeure() {
    return heure;
}

public void setHeure(String heure) {
    this.heure = heure;
}

public String getMotif() {
    return motif;
}

public void setMotif(String motif) {
    this.motif = motif;
}

public Patient getIdpat() {
    return idpat;
}

public void setIdpat(Patient idpat) {
    this.idpat = idpat;
}

public Medecin getIdmed() {
    return idmed;
}

public void setIdmed(Medecin idmed) {
    this.idmed = idmed;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (idrdv != null ? idrdv.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 Rendezvous)) {
        return false;
    }
    Rendezvous other = (Rendezvous) object;
    if ((this.idrdv == null && other.idrdv != null) || (this.idrdv != null && !this.idrdv.equals(other.idrdv))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "mapping.Rendezvous[ idrdv=" + idrdv + " ]";
}

}

0 个答案:

没有答案