[英]oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp
[英]insert java.sql.Timestamp data in oracle timestamp(6) column using Hibernate
我想使用Struts2表单在Oracle数据库中的名为vol
的表中插入一行,但是insert动作返回"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();
}
使用以下代码将Timestamp
数据插入到Oracle类型的timestamp
vol.setHeureDepart(new Timestamp(dateFormat.parse(heureDepart).getTime()));
实体应将此列映射为
@Column(name = "heure_depart", length = 19)
private Date heureDepart;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.