在数据库中增加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;
}
谢谢哪位能指点一下我,谢了。
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;
}
解决方案 »
- 我把数据库由sql2000替换成了sql2008,jdk1.6替换成jdk1.7,出现了如下错误,求大神们指点迷津
- 求java写入txt解决中文乱码的的写法
- 急!!!!!!J2EE 把jsp放在WEB-INF访问路径是什么
- struts2文件上传拦截器问题
- SOA与Web Service
- hibernate 插入问题
- 在设计下载时出现了ClientAbortException: java.net.SocketException:
- 关于JavaMail的问题
- java中怎样获得oracel过程的结果集
- servlet中的out的一个问题
- structs2 中ServletActionContext.getRequest()的问题请教
- 简单输入验证的问题
问题原因:
原因是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就可以和数据库同步数据了,我贴出来供参考,希望大家一同分享。