在数据库中增加ID相同的一条数据后,数据库里可以看出已经增加成功,但我用hibernate以下两种方法查询相同ID的数据时,得到的还是之前查询的数据,新增加的时间没有被找出来,第二种我ssion.flush();还是没有新增的数据,这是为什么?怎么解决呢?
谢谢哪位能指点一下我,谢了。
public ResultInfo<BlkCategoryBlock> findCategoryBlockByBlockId(
Integer blockId,PageInfo pageInfo) {
StringBuffer hql = new StringBuffer();
hql.append("from BlkCategoryBlock b where b.blkBlock.blockId=");
hql.append(blockId);
hql.append(" order by b.id desc");
return find(hql.toString(), pageInfo);
}

public List<BlkCategoryBlock> findCategoryBlockByBlockId(Integer blockId) {
StringBuffer hql = new StringBuffer();
Session session=this.getHibernateTemplate().getSessionFactory().getCurrentSession();
Query query=session.createQuery("from BlkCategoryBlock b where b.blkBlock.blockId=? order by b.id desc");
query.setParameter(0,blockId);
List <BlkCategoryBlock>list=query.list();
session.flush();
return list;
}

解决方案 »

  1.   

    虽然贴出来没人回答我,但是通过我的努力终于解决了该问题
    问题原因:
    原因是hibernate中是配置了缓存
    <list>
    <value>.*find.*</value>
    <value>.*get.*</value>
    </list>
    所以find查出的数据会放入到缓存中,而我插入数据用的是存储过程做的,因为存储过程会直接将数据插入到数据库中而不会放入到缓存中,所以当再次查询数据时由于缓存中并没有新插入的数据,而该方法默认又会从缓存中查找数据,就导致了查出的数据没有最新插入的那条记录,
    解决方法:
    我将findCategoryBlockByBlockId的名字改成queryCategoryBlockByBlockId后就不能用hibernate中的getHibernateTemplate().getSessionFactory().getCurrentSession(); 得到session了,
    所以我换了种方法得到session
    Session session = this.getSessionFactory().openSession();
    Transaction tx=session.beginTransaction();
    Transaction tx=session.beginTransaction();
    Query query=session.createQuery("from BlkCategoryBlock b where b.blkBlock.blockId=? order by b.id desc");
    query.setParameter(0,blockId);
    list=query.list();
    session.flush();
    tx.commit();
    session.close();
    重新打开一个session就可以和数据库同步数据了,我贴出来供参考,希望大家一同分享。