我使用持久性和hql從oracle數據庫讀取日期到java代碼。然而,小時,分鐘和秒的值總是設置爲00Java持久性 - 無法從oracle表列讀取時間
這裏是我的代碼:
...
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HotelskoResenjeWSPersistence");
EntityManager em = emf.createEntityManager();
// what i read from the db is stored here :
List<PpaDrCdr> resultSet = null;
String queryString = "select "
+"id, to_timestamp(substr(chargestart, 1, 17), 'dd.mm.rr hh24:mi:ss') as chargestart "
+"from [email protected] t "
+"where t.answertime is not null "
+"and t.reportedduration > 0 "
+"and t.directorynumber = :mdn "
+"order by servicestart";
try{
Query query = em.createNativeQuery(queryString, PpaDrCdr.class);
resultSet = query
.setParameter("mdn", account.getMdn())
.getResultList();
}catch(HibernateException ex){
// processing exception here
ex.printStackTrace();
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
if(resultSet != null && !resultSet.isEmpty()){
for (PpaDrCdr row : resultSet) {
System.out.println("\n\t"+
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(row.getDate1())+
"\n");
}
}
...
這始終打印出來00:00:00對於每一個日期讀取時間從數據庫中,這是完全錯誤的。 我的JPA類是很簡單的:
@Entity
@Table(name = "ppa_dr_cdr")
public class PpaDrCdr {
@Id
@Column(name = "id")
private Long id;
@Column(name="chargestart")
@Temporal(TemporalType.TIMESTAMP)
private Date chargestart;
public Date getChargestart() {
return chargestart;
}
public void setChargestart(Date chargestart) {
this.chargestart = chargestart;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
...
爲什麼不持久讀取時間? 在此先感謝。
你確定在數據庫中有完整的日期和時間嗎?另外你的使用本機SQL查詢,而不是HQL。 –
爲什麼在使用JPA API時會硬編碼HibernateException? JPA API保證JPA異常,然後你的代碼是可移植的......嗯,它不是數據存儲可移植的,因爲你使用了hacky SQL,但這是另一回事 –