日期:2014-05-20  浏览次数:20617 次

hibernate缓存机制的问题
Dog.java
Java code

package gpf;

public class Dog {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


Dog.hbm.xml
XML code

<?xml version="1.0" encoding="utf-8"?>  
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping>  
    <class name="gpf.Dog" table="Dog">  
        <id name="id" type="java.lang.Integer">  
            <column name="id"/>  
            <generator class="native"></generator>  
        </id>  
        <property name="name" type="java.lang.String">  
            <column name="name"/>  
        </property>
    </class>
</hibernate-mapping>


hibernate.cfg.xml
XML code

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <property name="connection.pool_size">1</property>
        
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <property name="format_sql">true</property>
        <property name="show_sql">true</property>

        <mapping resource="gpf/Dog.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


测试类Test.java
Java code

public class Test {
    private Session session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession();
    
    @org.junit.Test
    public void test(){  
        session.beginTransaction();
        Query query = session.createQuery("from Dog");
        Iterator<Dog> iterator = query.iterate();
        session.getTransaction().commit();
    } 



请问第一次查询打印出来的sql语句不是应该是分步的吗,为什么我每次运行都只是发出一个select语句

------解决方案--------------------
简单的查询还要分什么步呢,query.iterate()就查询一次嘛,还要几个SELECT 要是迭代出数据比如做出相应的out打印就可以看出效果了