即使数据库中存在值,Hibernate查询也会返回空列表

时间:2018-06-27 05:37:46

标签: hibernate

我已经使用spring mvc和hibernate开发了一个应用程序。计划任务每​​2分钟更新一次我的数据库(mysql)的新数据,然后通过休眠查询检索此数据。注意到了一个问题,尽管我的数据库始终具有值,但有时休眠会从表中返回空列表,有时它会获取最新值。

这是仅在生产中出现的问题。 这就是我从javascript调用的方式,

$.ajax({
    type : "GET",
    contentType : "application/json",
    url : "/myapp/getData",
    dataType : 'json',
    timeout : 100000,
    success : function(res) {
        console.log("result: ", res);
        //process data
    },
    error : function(e) {
        console.log("ERROR: ", e);
    },
    done : function(e) {
        console.log("DONE");
    }
});

还有我的Java代码:

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date sDate = new Date();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();
        List<Weather> wlist = null;
        Date fDate=getStartOfDay(sDate);
        Date tDate=getEndOfDay(sDate);
        Query query = session.createQuery("from Weather wr WHERE wr.addDate BETWEEN :stDate AND :endDate ORDER BY id desc");
        query.setParameter("stDate", sdf1.parse(sdf1.format(fDate)));
        query.setParameter("endDate", sdf1.parse(sdf1.format(tDate)));
        query.setMaxResults(1);
        wlist = query.list();
        session.clear();
        session.close();
        return wlist;

休眠映射:

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
               <property name="hibernate.cache.use_second_level_cache">false</property>
               <property name="hibernate.cache.use_query_cache">false</property> 
               <property name="show_sql">true</property>
               <property name="hibernate.connection.CharSet">utf8</property>
               <property name="hibernate.connection.characterEncoding">utf8</property>
               <property name="hibernate.connection.useUnicode">true</property>
               <property name="hibernate.connection.driver_class">
                  com.mysql.jdbc.Driver
               </property>

我有时会得到数据,有时会得到空列表。请帮忙。 生产设置还需要做更多的事情

0 个答案:

没有答案
相关问题