使用Hibernate在oracle timestamp(6)列中插入java.sql.Timestamp数据

时间:2014-03-10 11:30:53

标签: java sql oracle hibernate struts2

我想使用Struts2表单在我的Oracle数据库中的一个名为vol的表中插入一行但插入操作返回"input"并且不执行插入 这是Vol.java代码:

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(){

}

}

这是表格的代码:

<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>

这是动作的执行方法:

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;
}

最后是方法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();
}

1 个答案:

答案 0 :(得分:0)

使用以下代码将Timestamp数据插入timestamp Oracle类型

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

实体应该将此列映射为

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