小弟刚从net转到java
今天用hibernate时候遇到一个郁闷无比问题
没用spring,struts这些东西,,就是简单的jsp+servlet+hibernate..
我用hibernate的分页查询时候,第一次查询没有问题,但如果当手动在数据库里添加一条记录之后,死活也查询不出来最新的那条记录,只能显示第一次查询的数据,,只有重启tomcat再访问才能查询出所有数据。
其中的分页查询代码如下:
/*
 * 根据档案分类查询该档案下的档案信息
 */
public PageBean findAllArchivesList(String archType,String filter, int showpage) throws Exception {
PageBean pageBean = new PageBean(10);

String sql = "  from Archivesinfo a where a.acid='"+archType+"'"; 

if(filter!=null){
if(!filter.equals("")){
sql = "  from Archivesinfo a  where  a.acid='"+archType+"' and a.archivesName like '%"+filter+"%'"; 
}
}

String sqlCount = "select count(*) " + sql;
// 得到总记录数
org.hibernate.Session sx=this.getSession();
org.hibernate.Query query = sx.createQuery(sqlCount);
Iterator iterCount = query.list().iterator();
if (iterCount.hasNext()) {
Object o=iterCount.next();
//Integer sCount = (Integer) iterCount.next();
//pageBean.setAllRecord(sCount.intValue());
pageBean.setAllRecord(Integer.parseInt(o.toString()));
pageBean.setCouPage(showpage);
}
// 得到显示的数据
int sta = (pageBean.getCouPage() - 1) * PageBean.getShowRecord();
int end = PageBean.getShowRecord();
if (PageBean.getShowRecord() >= pageBean.getAllRecord()) {
query = sx.createQuery(sql);
} else {
query = sx.createQuery(sql).setFirstResult(
(pageBean.getCouPage() - 1) * PageBean.getShowRecord())
.setMaxResults(PageBean.getShowRecord());
}
pageBean.setDateList(query.list());
//sx.evict(new Archivesinfo());
sx.clear();

sx.close();

return pageBean;
}
sx.clear()和close()方法都是遇到这个问题之后加上去想试试的。可是还是不行,,
我的hibernate配置文件如下。。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="connection.username">root</property>
    <property name="connection.password">admin</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="hibernate.connection.provider_class">org.hibernate.connection.DriverManagerConnectionProvider</property>
    <!--
      property name="connection.url">jdbc:mysql://localhost:3306/yakcms</property>
      <property name="connection.username">root</property>
      <property name="connection.password">1111</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property
    -->
    <property name="show_sql">true</property>
    <property name="c3p0.min_size">5</property>
    <property name="c3p0.max_size">50</property>
    <property name="c3p0.time_out">1800</property>
    <property name="c3p0.max_statement">50</property>
    <property name="c3p0.preferredTestQuery">SELECT 1</property>
    <property name="c3p0.idleConnectionTestPeriod">18000</property>
    <property name="c3p0.maxIdleTime">25000</property>
    <property name="c3p0.testConnectionOnCheckout">true</property>
    <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
    <property name="connection.useUnicode">true</property>
    <property name="connection.characterEncoding">UTF-8</property> 
    <mapping resource="org/ligitalsoft/uim/config/Test.hbm.xml"/>
<mapping resource="org/dch/roadarchives/po/Archivesclasses.hbm.xml"/>
<mapping resource="org/dch/roadarchives/po/Archivesinfo.hbm.xml"/>
  </session-factory>
</hibernate-configuration>各位大虾帮帮忙,我真的是河马郁闷

解决方案 »

  1.   

    是你的分页有问题吧,Hibernate默认情况下是不启用二级缓存的,你这里的配置并没有配置启用缓存,所以应该不是缓存问题
    要将这个属性:hibernate.cache.use_query_cache配置成true才会启用二级缓存
      

  2.   


    // 得到总记录数 
    org.hibernate.Session sx=this.getSession();
    //插入这句
    net.sf.hibernate.Transaction tx = sx.beginTransaction();org.hibernate.Query query = sx.createQuery(sqlCount);
    ...
    ...
    ...
    //这个注释掉
    //sx.clear(); //加上这个
    tx.commit();sx.close(); 
     
    试试看,可能有效。
    还有一种可能是你手动插入数据库的数据没commit过?