【问题标题】:return date is wrong in hibernate休眠中的返回日期错误
【发布时间】:2013-08-30 14:22:47
【问题描述】:

我是休眠新手。我用的是MYSQL数据库。我的表中有日期字段。它的类型是日期。有一个值 1989-08-13。当我使用休眠获得价值时,它给出的日期为 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;

【问题讨论】:

  • 你还没有告诉我们哪个领域给你带来了问题,这没有帮助......
  • java 中有 2 个日期:java.util.Date 和 java.sql.Date... 确保不要混淆两者。
  • 618984000 在 UNIX 时间表示法中是 1989-08-13 12:00am。你所拥有的是相同的毫秒表示。
  • lastUpDate 列是给您带来问题的列吗?我注意到属性类型是时间戳,文件中是日期。希望这会有所帮助。
  • 我看不到任何整数类型的字段,staffId 除外。那么您能否发布将日期设为 6189498000000 的代码?

标签: java mysql sql hibernate


【解决方案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 相同。

【讨论】:

    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 2021-04-29
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多