zhrmghg2014
zhrmghg2014
2011-04-13 09:51
浏览 275
已采纳

用Hibernate映射mysql问题(Timestamp)

我知道这个是个常见问题,我已经看了论坛帖子和查阅相关资料,始终没找到好的解决方法,如果有空的帮解答下吧。

SessionInfo.hbm.xml

[code="java"]









[/code]

SessionInfoDao.java

[code="java"]public boolean sessionInfoExists(SessionInfo info) throws RuntimeException {
if (info == null) {
return false;
}

    Session sess = DaoBase.currentSession();
    try {
        String infoEmail = info.getEmail();
        String infoClientIP = info.getClientIP();
        Date infoCreationTime = info.getCreationTime();

        if (infoEmail == null || infoClientIP == null || infoCreationTime == null) {
            return false;
        }

        int diff = Util.diffDays(new Date(), infoCreationTime);
        if (diff < 0 || diff > SessionInfo.COOKIE_MAX_DAYS) {
            return false;
        }

        Query q = sess.createQuery("from SessionInfo where email = :email and clientIP = :clientIP and creationTime =:creationTime")
                .setString("email", infoEmail)
                .setString("clientIP", infoClientIP)

.setStrng("creationTime", infoCreationTime);
Iterator itr = q.iterate();
if (itr.hasNext()) {
return true;

        } else {
            return false;
        }
    } finally {
        DaoBase.closeSession();
    }[/code]

SessionInfoDaoTest.java

[code="java"]@Test
public void testSessionInfoExists() {
SessionInfo info = null;
assertFalse(dao.sessionInfoExists(info));
info = new SessionInfo();
assertFalse(dao.sessionInfoExists(info));
info.setEmail("test@dc.com");
assertFalse(dao.sessionInfoExists(info));
info.setSessionID("123456");
assertFalse(dao.sessionInfoExists(info));
info.setClientIP("192.168.1.1");
assertFalse(dao.sessionInfoExists(info));
info.setCreationTime(new Date());
assertFalse(dao.sessionInfoExists(info));

    info = new SessionInfo();
    info.setEmail("test@dc.com");
    info.setClientIP("192.168.1.1");
    info.setCreationTime(new Date());
    Session sess = reopenSession();
    sess.save(info);
    sess.flush();
    closeSession();
    assertTrue(dao.sessionInfoExists(info));
}[/code]

这里主要是timeStamp的问题,如果我把hql里面的creationTime去掉,即:"from SessionInfo where email = :email and clientIP = :clientIP " 我的测试就没问题,我把它加上后即使转成sqlDate也没用,不知道问题出在哪里?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • cheng888qi
    cheng888qi 2011-04-13 11:45
    已采纳

    [quote]


    [/quote]
    这里映射的是timestamp类型,
    [quote]
    .setString("creationTime", infoCreationTime);

    [/quote]
    肯定不正确,你用下
    [code="java"]
    .setTimestamp("creationTime", infoCreationTime);
    [/code]

    点赞 评论

相关推荐