休眠时返回日期是错误的

时间:2013-08-27 17:44:34

标签: java mysql sql hibernate

我是hibernate的新手。我用过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;

1 个答案:

答案 0 :(得分:1)

在您的hbm文件中,有“timestamp”列类型。让hibernate猜测列类型:

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

换句话说,如果您有以下属性:

java.util.Date createDate;

hibernate3-maven-plugin hibernate3:hbm2ddl目标将在MySql中创建以下列:

`CreateDate` datetime DEFAULT NULL

注意:日期时间和时间戳具有不同的范围。 Java Long(时间戳)和java.util.Date相同。