问题描述
我知道这个是个常见问题,我已经看了论坛帖子和查阅相关资料,始终没找到好的解决方法,如果有空的帮解答下吧。SessionInfo.hbm.xml<hibernate-mapping package="domaininvestment.model"><class name="SessionInfo" table="SessionInfo"><id name="id"><generator class="native" /></id><property name="email" type="string" length="500" not-null="true" /><property name="sessionID" type="string" length="200" /><property name="clientIP" type="string" length="200" not-null="true" /><property name="creationTime" type="timestamp" not-null="true" /></class></hibernate-mapping>SessionInfoDao.javapublic 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();}SessionInfoDaoTest.java@Testpublic 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));}这里主要是timeStamp的问题,如果我把hql里面的creationTime去掉,即:"from SessionInfo where email = :email and clientIP = :clientIP " 我的测试就没问题,我把它加上后即使转成sqlDate也没用,不知道问题出在哪里? 问题补充:cheng888qi 写道
解决方案
引用<property name="creationTime" type="timestamp" not-null="true" /> 这里映射的是timestamp类型,引用 .setString("creationTime", infoCreationTime); 肯定不正确,你用下.setTimestamp("creationTime", infoCreationTime);