2013-08-27 102 views
0

我是休眠的新手。我使用MYSQL數據庫。我的桌子上有日期字段。它的類型是DATE。 1989-08-13有一個值。當我使用hibernate獲得價值時,它將該日期設爲6189498000000.我想將價值作爲實際日期(1989-08-13)。請幫我休眠時返回日期錯誤

這是我的xml文件

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<!-- Generated Aug 27, 2013 1:03:09 AM by Hibernate Tools 4.0.0 --> 
<hibernate-mapping> 
    <class name="core.classes.Staff" table="staff" catalog="surgercare"> 
     <id name="staffId" type="int"> 
      <column name="staff_ID" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="staffName" type="string"> 
      <column name="staff_Name" length="150" /> 
     </property> 
     <property name="staffDesignation" type="string"> 
      <column name="staff_designation" length="50" /> 
     </property> 
     <property name="createDate" type="timestamp"> 
      <column name="CreateDate" length="19" /> 
     </property> 
     <property name="createUser" type="string"> 
      <column name="CreateUser" length="200" /> 
     </property> 
     <property name="lastUpDate" type="timestamp"> 
      <column name="LastUpDate" length="19" /> 
     </property> 
     <property name="lastUpDateUser" type="string"> 
      <column name="LastUpDateUser" length="200" /> 
     </property> 
    </class> 
</hibernate-mapping> 

我的類文件

import java.util.Date; 

public class Staff implements java.io.Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private int staffId; 
    private String staffName; 
    private String staffDesignation; 
    private Date createDate; 
    private String createUser; 
    private Date lastUpDate; 
    private String lastUpDateUser; 

    public Staff() { 
    } 

    public Staff(int staffId) { 
     this.staffId = staffId; 
    } 

    public Staff(int staffId, String staffName, String staffDesignation, 
      Date createDate, String createUser, Date lastUpDate, 
      String lastUpDateUser) { 
     this.staffId = staffId; 
     this.staffName = staffName; 
     this.staffDesignation = staffDesignation; 
     this.createDate = createDate; 
     this.createUser = createUser; 
     this.lastUpDate = lastUpDate; 
     this.lastUpDateUser = lastUpDateUser; 
    } 

    public int getStaffId() { 
     return this.staffId; 
    } 

    public void setStaffId(int staffId) { 
     this.staffId = staffId; 
    } 

    public String getStaffName() { 
     return this.staffName; 
    } 

    public void setStaffName(String staffName) { 
     this.staffName = staffName; 
    } 

    public String getStaffDesignation() { 
     return this.staffDesignation; 
    } 

    public void setStaffDesignation(String staffDesignation) { 
     this.staffDesignation = staffDesignation; 
    } 

    public Date getCreateDate() { 
     return this.createDate; 
    } 

    public void setCreateDate(Date createDate) { 
     this.createDate = createDate; 
    } 

    public String getCreateUser() { 
     return this.createUser; 
    } 

    public void setCreateUser(String createUser) { 
     this.createUser = createUser; 
    } 

    public Date getLastUpDate() { 
     return this.lastUpDate; 
    } 

    public void setLastUpDate(Date lastUpDate) { 
     this.lastUpDate = lastUpDate; 
    } 

    public String getLastUpDateUser() { 
     return this.lastUpDateUser; 
    } 

    public void setLastUpDateUser(String lastUpDateUser) { 
     this.lastUpDateUser = lastUpDateUser; 
    } 

} 

這是我的控制器

tx = ses.beginTransaction(); 
Query query = ses.createQuery("select s from Staff as s where s.staffId = :ID"); 
query.setString("ID", docID); 
List<Staff> DocDetailsList = castlist(Staff.class,query.list()); 
tx.commit(); 
return DocDetailsList; 
+0

您還沒有告訴我們哪個字段給您帶來問題,這並沒有幫助... –

+0

java中有兩個日期:java.util.Date和java.sql.Date ...確保您不會混淆兩者。 – codeMan

+2

618984000是1989-08-13 12:00在UNIX時間表示法。你有什麼毫秒錶示相同。 – tadman

回答

1

在您的HBM文件中,有 「時間戳」列類型。讓Hibernate猜測列類型:

<property name="createDate" column="CreateDate" /> 

換句話說,如果你有以下屬性:

java.util.Date createDate; 

Hibernate3中,Maven的插件Hibernate3的:就是hbm2ddl目標將在MySQL創建以下列:

`CreateDate` datetime DEFAULT NULL 

注意:datetime和時間戳有不同的範圍。 Java Long(timestamp)和java.util.Date也一樣。