2017-04-08 39 views
0

我使用持久性和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; 
     } 
    } 
... 

爲什麼不持久讀取時間? 在此先感謝。

+0

你確定在數據庫中有完整的日期和時間嗎?另外你的使用本機SQL查詢,而不是HQL。 –

+1

爲什麼在使用JPA API時會硬編碼HibernateException? JPA API保證JPA異常,然後你的代碼是可移植的......嗯,它不是數據存儲可移植的,因爲你使用了hacky SQL,但這是另一回事 –

回答

0

使用java.time.LocalDateTime

@Column(name="chargestart") 
    @Basic 
    private LocalDateTime chargestart; 

這已經是一個已知的基本數據類型在Hibernate中。 ( - >http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#basic

另外,我建議使用Hibernate的Criteria API進行查詢。避免使用SQL語句是使用Hibernate的好處之一。

+0

@Basic沒有工作。 – Lazaruss

+0

我必須使用查詢,因爲該表有23列,我只需要特定的值。不過,我會通讀你發佈的教程。也許我會想到一些事情。 – Lazaruss

+0

問題是,從數據庫讀取的每個時間戳都重置爲午夜(00:00:00),位於我的查詢和我的類變量設置之間,我不知道爲什麼。 – Lazaruss

0

好的。我終於解決了它。我拋棄了實體類,加載從數據庫中的一切作爲List<Object[]>的結果集,然後澆注單個對象到我需要什麼:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPersistence"); 
EntityManager em = emf.createEntityManager(); 

List<Object[]> oList = new ArrayList<Object[]>(); 

try{ 
    Session s1 = (Session) em.getDelegate(); 
    oList = s1.getNamedQuery("MyNamedQueryFromOrmXml") 
        .setParameter("mdn", account.getMdn()) 
        .list(); 
}catch(HibernateException ex){ 
    ex.printStackTrace(); 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext("classpath:beans.xml"); 
    GenericFaultType gft = ((HibernateExceptionToGFTConverter)context.getBean("GFTbean")).convert(ex); 

    throw new GetCDREntryListFaultMsg("HibernateException occurred", gft, ex); 
}catch (Exception e){ 
    e.printStackTrace(); 
    throw new RuntimeException(e); 
} 

// this is what i will need : 
List<CDREntry> cdrE = new ArrayList<CDREntry>(); 
for(Object[] o : oList){ 
    if(o!=null){ 
     try{ 
      CDREntry pom = new CDREntry(); 

      pom.setId((long)o[0]); 
      // AND NOW I GET TIME TOO !!! 
      pom.setChargingStart((Timestamp)o[1]); 
      //... 
      cdrE.add(pom); 
     }catch(Exception e){ 
      System.out.print("\n\tFailed to cast "+e.getMessage()); 
     } 
    } 
} 

不知道爲什麼老一沒有工作,但這裏是現在它如何工作,使用session和orm.xml