简体   繁体   中英

insert java.sql.Timestamp data in oracle timestamp(6) column using Hibernate

I want to insert a line in a table called vol in my Oracle database using a Struts2 form but the insert action returns "input" and don't execute the insertion this is the Vol.java code:

package models;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="vol")
public class Vol {
@Id
private int idVol;
private String typeAvion;
private String nomAvion;
private String depart;
private String destination;
@Temporal(TemporalType.TIMESTAMP)
private Date heureDepart;
private String heureArrivee;
private String technicien;

public int getIdVol() {
return idVol;
}

public void setIdVol(int idVol) {
this.idVol = idVol;
}

public String getTypeAvion() {
return typeAvion;
}

public void setTypeAvion(String typeAvion) {
this.typeAvion = typeAvion;
}

public String getNomAvion() {
return nomAvion;
}

public void setNomAvion(String nomAvion) {
this.nomAvion = nomAvion;
}

public String getDepart() {
return depart;
}

public void setDepart(String depart) {
this.depart = depart;
}

public String getDestination() {
return destination;
}

public void setDestination(String destination) {
this.destination = destination;
}

public Date getHeureDepart() {
return heureDepart;
}

public void setHeureDepart(Date heureDepart) {
this.heureDepart = heureDepart;
}

public String getheureArrivee() {
return heureArrivee;
}

public void setheureArrivee(String heureArrivee) {
this.heureArrivee = heureArrivee;
}

public String getTechnicien() {
return technicien;
}

public void setTechnicien(String technicien) {
this.technicien = technicien;
}



public Vol(){

}

}

this is the code of the form:

<s:form action="ajoutVol" method="post">
<s:textfield name="vol.idVol" label="idVol" size="20" />
<s:textfield name="vol.typeAvion" label="Type Avion" size="20" />
<s:textfield name="vol.nomAvion" label="Nom Avion" size="20" />
<s:textfield name="vol.depart" label="Depart" size="20" />
<s:textfield name="vol.destination" label="Destination" size="20" />
<s:textfield name="heureDepart" label="Heure de dapart" size="20" />
<s:textfield name="vol.heureArrivee" label="Heure d'arrivée" size="20" />
<s:textfield name="vol.technicien" label="technicien" size="20" />
<s:submit name="submit" label="Submit"  />
</s:form>

this is the execute method of the action:

public String execute(){
    VolDAO vd= new VolDAO();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    try {
        vol.setHeureDepart(dateFormat.parse(heureDepart));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    vd.insertVol(vol);
    Map session = ActionContext.getContext().getSession();
    session.put("vol", vol);
    return SUCCESS;
}

and finally the code of the method insertVol() :

public void insertVol(Vol vol){
    SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
    Session session=sessionFactory.openSession();
    session.beginTransaction();
    session.save(vol);
    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

Use the following code to insert Timestamp data to timestamp Oracle type

vol.setHeureDepart(new Timestamp(dateFormat.parse(heureDepart).getTime()));

the entity should map this column like

@Column(name = "heure_depart", length = 19)
private Date heureDepart;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM