67,513
社区成员
发帖
与我相关
我的任务
分享
<hibernate-mapping package="hiber.domain">
<class name="Person" >
<cache usage="read-write"/>
<id name="id">
<generator class="increment"/>
</id>
<property name="name" not-null="true"/>
</class>
</hibernate-mapping>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.generate_statistics">true</property>
<cache name="hiber.domain.Person"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
public class One2One {
public static void main(String[] args) {
addPersonAndIdCard2();
check();
}
public static void addPersonAndIdCard2() {
Session s= HiberUtil.getSession();
Transaction t = s.beginTransaction();
try{
Person person = new Person();
person.setName("sunyaozong");
s.save(person);
t.commit();
}catch(HibernateException e){
if(t!=null){
t.rollback();
}
throw e;
}finally{
if(s!=null){
s.close();
}
}
}
public static void check(){
Session s= null;
Transaction t =null;
try{
s= HiberUtil.getSession();
t = s.beginTransaction();
//s.getSessionFactory().evict(Person.class); //语句(1)
Person p1 =(Person)s.get(Person.class, 1);//语句(2)
System.out.println(""+p1.getName());
s.clear(); //语句(3)
Person p2 =(Person)s.get(Person.class, 1); //语句(4)
System.out.println(""+p2.getName());
}catch(HibernateException e){
if(t!=null){
t.rollback();
}
throw e;
}finally{
if(s!=null){
s.close();
}
}
try{
s= HiberUtil.getSession();
t = s.beginTransaction();
s.getSessionFactory().evict(Person.class);//语句(5)
Person p2 =(Person)s.get(Person.class, 1); //语句(6)
System.out.println(""+p2.getName());
}catch(HibernateException e){
if(t!=null){
t.rollback();
}
throw e;
}finally{
if(s!=null){
s.close();
}
}
Statistics st =s.getSessionFactory().getStatistics();
System.out.println(st);
System.out.println("put:"+st.getSecondLevelCachePutCount());
System.out.println("hit:"+st.getSecondLevelCacheHitCount());
System.out.println("miss:"+st.getSecondLevelCacheMissCount());
}
}
Hibernate: select idcard0_.person_id as person1_3_0_, idcard0_.name as name3_0_ from idcard idcard0_ where idcard0_.person_id=?
sunyaozong
Hibernate: select idcard0_.person_id as person1_3_0_, idcard0_.name as name3_0_ from idcard idcard0_ where idcard0_.person_id=?
sunyaozong
Hibernate: select person0_.id as id2_1_, person0_.name as name2_1_, idcard1_.person_id as person1_3_0_, idcard1_.name as name3_0_ from Person person0_ left outer join idcard idcard1_ on person0_.id=idcard1_.person_id where person0_.id=?
sunyaozong
Statistics[start time=1268924734687,sessions opened=3,sessions closed=3,transactions=1,successful transactions=1,optimistic lock failures=0,flushes=1,connections obtained=3,statements prepared=5,statements closed=5,second level cache puts=2,second level cache hits=2,second level cache misses=1,entities loaded=1,entities updated=0,entities inserted=1,entities deleted=0,entities fetched=2,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
put:2
hit:2
miss:1