用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个回答

[quote]


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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐