《深入浅出Hibernate》书中的一个错误,误导了我好久。

[b][color=red]后来想了想,有可能是因为hibernate的版本不一样吧,忘了在这说了。谢谢[url="http://xujunj2ee.iteye.com"]xujunJ2EE[/url]同学[url="http://yuan.iteye.com/blog/206764#comments"]提醒[/url]。暂时没有时间去测试3.0的版本了。下面的内容是在hibernate3.2下测试的[/color][/b]


3.load方法可以充分利用内部缓存和二级缓存的现有数据,而get方法则仅仅在内部缓存中进行数据查找,如没有发现对应数据,将越过二级缓存,直接调用SQL完成数据读取。
==========================================
以上是深入浅出Hibernate一书第245页中的一部分。
以前因为读过这本书,所以没有使用过get()方法,一直用load()。这两天认真的重新读了一遍,并且做了试验,发现这一句有误。我写了代码测试了,两个不同session中对同一条记录的查询动作,不论是get还是load,都只输出一句sql,也就是说get和load都利用到了二级缓存。

附上测试代码:
TestCase(JUnit4)
package test.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import test.hibernate.entity.Member;

/**
* @(#)HibernateTest.java 2008-6-21 下午04:43:55
*
* @author Yuan
* 类说明
*/
public class HibernateTest {

private static SessionFactory sessionFactory;

private static Session session;

@Before
public void setUp(){
sessionFactory = new Configuration().configure().buildSessionFactory();
}

@After
public void tearDown(){
sessionFactory.close();
}
/**
* 测试Session.load()
*/
@Test
public void testLoad(){
System.out.println("**********************Load**********************");

openSessionAndBeginTransaction();
((Member)session.load(Member.class, "402880b31aab576f011aab5771bc0005")).getMemberName();
commitTransactionAndCloseSession();

openSessionAndBeginTransaction();
((Member)session.load(Member.class, "402880b31aab576f011aab5771bc0005")).getMemberName();
commitTransactionAndCloseSession();
}

/**
* 测试Session.get()
*/
@Test
public void testGet(){
System.out.println("**********************Get**********************");

openSessionAndBeginTransaction();
session.get(Member.class, "402880b31aab576f011aab5771bc0005");
commitTransactionAndCloseSession();

openSessionAndBeginTransaction();
session.get(Member.class, "402880b31aab576f011aab5771bc0005");
commitTransactionAndCloseSession();
}

private void openSessionAndBeginTransaction(){
session = sessionFactory.openSession();
session.beginTransaction();
}

private void commitTransactionAndCloseSession(){
session.getTransaction().commit();
session.flush();
session.close();
}
}

Entity
package test.hibernate.entity;

import java.io.Serializable;

/**
* @(#)Member.java 2008-6-21 下午04:47:26
*
* @author Yuan
* Member Entity
*/
public class Member implements Serializable {

private static final long serialVersionUID = 3667540833969806410L;

private String memberID;

private String memberName;

private String password;

private String realName;

public String getMemberID() {
return memberID;
}

public void setMemberID(String memberID) {
this.memberID = memberID;
}

public String getMemberName() {
return memberName;
}

public void setMemberName(String memberName) {
this.memberName = memberName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getRealName() {
return realName;
}

public void setRealName(String realName) {
this.realName = realName;
}
}

Member.hbm.xml
<?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="test.hibernate.entity.Member" table="t_member">
<cache usage="read-write"/>
<id name="memberID" type="string" length="32">
<generator class="uuid"/>
</id>
<property name="memberName" type="string" length="32" not-null="true"/>
<property name="password" type="string" length="32" not-null="true"/>
<property name="realName" type="string" length="32"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/test
</property>

<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>

<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">******</property>

<property name="hibernate.show_sql">true</property>

<property name="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider
</property>

<!--property name="hibernate.hbm2ddl.auto">
create
</property-->

<mapping resource="test/hibernate/entity/Member.hbm.xml"/>
</session-factory>
</hibernate-configuration>

ehcache.xml
<?xml version="1.0" encoding="utf-8"?>
<ehcache>
<diskStore path = "java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"/>
</ehcache>
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页