hibernateTemplate分页数据不同步问题
这个类是hibernate映射时自动生成的DAO类;
public class TCompanyDAO extends HibernateDaoSupport {
??? private static final Log log = LogFactory.getLog(TCompanyDAO.class);
??? //(1)获得会话
?? ??? private static HibernateTemplate hibernateTemplate=new HibernateTemplate(HibernateSessionFactory.getSessionFactory());
?
//(2)
??? protected void initDao() {
??? ??? // do nothing??? ??
??? }
?
?
?public static List getCompanyListForPage(final int offset, final int lengh) {????
??????? log.debug("finding getCompanyListForPage分页查询");
??????? try {
??????????? List list = hibernateTemplate.executeFind(new HibernateCallback(){??????????? ???
??????????? ??? String queryString = "from TCompany";
??????????????? public Object doInHibernate(Session session)
??????????????????????? throws HibernateException, SQLException {??????????????? ???
??????????????????? List list2 = session.createQuery(queryString)
??????????????????????????? .setFirstResult(offset)
??????????????????????????? .setMaxResults(lengh)
??????????????????????????? .list();
??????????????????? return list2;
??????????????? }});
???????????
??????????? return list;
??????? } catch (RuntimeException re) {
??????? ???
??????????? log.error("find ListForPage failed", re);
??????????? throw re;
??????? }
??? }
?
}
?
?
以上的代码,在程序运行时,用第三方软件给数据插入时据时,这个方法获取不到最新的数据,
解决方法如下:
我要作两个地方的修改;
看上面的红色字:
(1) //获得会话
??? private static HibernateTemplate hibernateTemplate;
?
(2)??? protected void initDao() {
??? ??? // do nothing??? ?
??????? hibernateTemplate=getHibernateTemplate();//这个方法是继承HibernateDaoSupport 类的
??? }
?
?
?
修改完这两个地方之后就可以调用了,数据还会同步