比较hibernate映射日期? [英] Comparing hibernate-mapped dates?

查看:103
本文介绍了比较hibernate映射日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Hibernate将日期从java对象映射到数据库?我尝试不同的方法,但我不满意他们。为什么?让我解释一下我的问题。我有以下类[1],包括我调用的主要方法和以下映射[2]。关于这种方法的问题,您可以看到控制台输出。

How can i map a date from a java object to a database with Hibernate? I try different approaches, but i am not happy with them. Why? Let me explain my issue. I have the following class [1] including the main method i invoke and with the following mapping [2]. The issue about this approach you can see, when you look at the console output.


false

false

false

1

-1

1224754335648

1224754335648

1224754335000

1224754335000

Thu Oct 23 11:32:15 CEST 2008

Thu Oct 23 11:32:15 CEST 2008

时钟@ 67064

正如你可以看到的日期并不完全相同,虽然他们应该,所以很难比较他们没有goofing的返回值 getTime 。我也在映射中尝试了java.sql.Date,Timestamp和date而不是timestamp,但没有成功。

As you can see the the to dates are not exactly equal, although they should, so it is hard to compare them without goofing around with return value of getTime. I also tried java.sql.Date, Timestamp and date instead of timestamp in the mapping, but without success.

我不知道为什么最后三位数字为零,如果这是一个休眠或java问题或我自己的愚蠢。

I wonder why the last three digits are zero and if this is a hibernate or a java issue or my own stupidity.

感谢您阅读。

[1]

public class Clock {

    int id;
    java.util.Date date;

    public static void main(String[] args) {
        HibernateUtil.init();
        HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

        Clock clock = new Clock();
        clock.date = new java.util.Date();

        HibernateUtil.getSessionFactory().getCurrentSession().saveOrUpdate(clock);
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();

        HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

        Clock fromDBClock = (Clock)HibernateUtil.getSessionFactory()
                        .getCurrentSession().get(Clock.class, 1);

        System.out.println(clock.date.equals(fromDBClock.date));
        System.out.println(fromDBClock.date.equals(clock.date));

        System.out.println(clock.date.compareTo(fromDBClock.date));
        System.out.println(fromDBClock.date.compareTo(clock.date));

        System.out.println(clock.date.getTime());
        System.out.println(fromDBClock.date.getTime());

        System.out.println(clock.date.toString());
        System.out.println(fromDBClock.toString());

        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();

        HibernateUtil.end();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public java.util.Date getDate() {
        return date;
    }

    public void setDate(java.util.Date date) {
        this.date = date;
    }


}

[2]

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="Clock" table="CLOCK">
        <id name="id" column="CLOCK_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp"/>


    </class>

</hibernate-mapping>


推荐答案

MySql的DateTime精度只有到了第二个。 Java日期精度是毫秒。
这就是为什么最后三位数字在数据库被放入数据库后为零。

MySql DateTime precision is only to the second. Java Date precision is to the millisecond. That is why the last three digits are zeros after it has been put in the database.

这样做到你原来的日期:

Do this to your original Date:

date = date.setTime((date.getTime()/ 1000)* 1000);

date = date.setTime((date.getTime() / 1000) * 1000);

这将设置为最后确切的第二,所有的比较将匹配。

This will set it to the last exact second, and all your comparisons will then match.

(BTW,
System.out.println(fromDBClock.toString());
应该是
System.out.println(fromDBClock.date.toString());

(BTW, System.out.println(fromDBClock.toString()); should be System.out.println(fromDBClock.date.toString());

这篇关于比较hibernate映射日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆