??xml version="1.0" encoding="utf-8" standalone="yes"?>国产a∨精品一区二区三区不卡 ,快she精品国产999,日本伊人色综合网http://www.aygfsteel.com/lsbwahaha/category/38817.htmlzh-cnMon, 06 Apr 2009 04:18:32 GMTMon, 06 Apr 2009 04:18:32 GMT60hibernate二~存http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264086.html胡鹏胡鹏Mon, 06 Apr 2009 04:02:00 GMThttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264086.htmlhttp://www.aygfsteel.com/lsbwahaha/comments/264086.htmlhttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264086.html#Feedback0http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264086.htmlhttp://www.aygfsteel.com/lsbwahaha/services/trackbacks/264086.htmlhibernate 二~存Q?~存的是实体对象Q二U缓存是攑֏化不是很大的数据)








二~存也称q程U的~存或SessionFactoryU的~存Q而二U缓存可以被所有的session(hibernate中的)׃n二~存的生命周期和SessionFactory的生命周期一_SessionFactory可以理二~存

 

二~存的配|和使用Q?/p>

1.echcache.xml文g拯到src? 二~存hibernate默认是开启的Q手动开?/p>

2.开启二U缓存,修改hibernate.cfg.xml文g,

<property name=”hibernate.cache.user_second_level_cache”>true</property>

3.指定~存产品提供?/p>

<property name=”hibernate.cache.provider_calss”>org.hibernate.cache.EhCacheProvider</property>

 

4.指定那些实体cM用二U缓存(两种ҎQ推荐用第二种Q?/p>

W一U:?.hbm.xml中,?lt;id>之前加入

<cache usage=”read-only” />, 使用二~存

W二U:在hibernate.cfg.xml配置文g??lt;mapping resource=”com/Studnet.hbm.xml” />后面加上Q?/p>

<class-cache class=” com.Studnet” usage=”read-only” /> 

 

二~存是缓存实体对象的

了解一U缓存和二~存的交?/p>

 

 

 

 

试二~存Q?/strong>

一Q开启两?/strong>session中发Z?/strong>load查询Q?/strong>get?/strong>load一P同样不会查询数据库),

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发Zơload查询Q第一ơload的时候不会去查询数据库,因ؓ他是LAZY的,当用的时候才L询数据库Q?nbsp; W二ơload的时候也不会Q当使用的时候查询数据库Q开启了二~存Q也不会查询数据库?/p>

 

 

二.开启两?/strong>sessionQ分别调?/strong>loadQ再使用sessionFactory清楚二~存

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

SessionFactory factory = HibernateUtil.getSessionFactory();

//factory.evict(Student.class); //清除所有Student对象

Factory.evict(Student.class,1); //清除指定id=1 的对?/p>

 

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发Zơload查询Q第一ơload的时候不会去查询数据库,因ؓ他是LAZY的,当用的时候才L询数据库Q?nbsp; W二ơload的时候也不会Q当使用的时候查询数据库Q它要查询数据库Q因ZU缓存中被清除了

 

三.一U缓存和二~存的交?/strong>

 

session.setCacheMode(CacheMode.GET);    //讄?只是从二U缓存里?不向二~存里写数据

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

SessionFactory factory = HibernateUtil.getSessionFactory();

//factory.evict(Student.class); //清除所有Student对象

Factory.evict(Student.class,1); //清除指定id=1 的对?/p>

 

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发Zơload查询Q第一ơload的时候不会去查询数据库,因ؓ他是LAZY的,当用的时候才L询数据库Q?nbsp; W二ơload的时候也不会Q当使用的时候查询数据库Q它要查询数据库Q因?讄了CacheMode为GET,(load讄成不能往二~冲中写数据), 所以二U缓冲中没有数据

 

 

 

session.setCacheMode(CacheMode.PUT);  //讄成只是向二~存里写数据Q不L?/p>

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

SessionFactory factory = HibernateUtil.getSessionFactory();

//factory.evict(Student.class); //清除所有Student对象

Factory.evict(Student.class,1); //清除指定id=1 的对?/p>

 

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发Zơload查询Q第一ơload的时候不会去查询数据库,因ؓ他是LAZY的,当用的时候才L询数据库Q?nbsp; W二ơload的时候也不会Q当使用的时候查询数据库Q它要查询数据库Q因|了CacheMode为POST,(load讄成只是向二~存里写数据Q不L?

 



胡鹏 2009-04-06 12:02 发表评论
]]>
hibernate一U缓?/title><link>http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264085.html</link><dc:creator>胡鹏</dc:creator><author>胡鹏</author><pubDate>Mon, 06 Apr 2009 03:59:00 GMT</pubDate><guid>http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264085.html</guid><wfw:comment>http://www.aygfsteel.com/lsbwahaha/comments/264085.html</wfw:comment><comments>http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264085.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264085.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lsbwahaha/services/trackbacks/264085.html</trackback:ping><description><![CDATA[<p>hibernate 一U缓存:(~存的是实体对象)</p> <p>一U缓存很短和session的生命周期一_一U缓存也叫sessionU的~存或事务缓?/p> <p><strong> <wbr></strong></p> <p>哪些Ҏ支持一U缓存:</p> <p>*get()</p> <p>*load()</p> <p>*iterate() <wbr> (查询实体对象)</p> <p> <wbr></p> <p>如何理一U缓存:</p> <p>* session.clear() session.evict()</p> <p> <wbr></p> <p>如何避免一ơ性大量的实体数据入库D内存溢出</p> <p>*先flushQ再clear</p> <p> <wbr></p> <p>如果数据量特别大Q考虑采用jdbc实现Q如果jdbc也不能满求,可以考虑采用数据库本w的特定导入工具</p> <p> <wbr></p> <p><strong>一Q?/strong><strong>Load</strong><strong>试</strong><strong>:</strong> <strong>在同一?/strong><strong>session</strong><strong>中发Z?/strong><strong>load</strong><strong>查询</strong></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Student sutdent = (Student)session.load(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> sutdent = (Student)session.load(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 在同一个session中发Zơload查询Q第一ơload的时候不会去查询数据库,因ؓ他是LAZY的,当用的时候才L询数据库Q?nbsp;<wbr> W二ơload的时候也不会Q当使用的时候也不会查询数据库,因ؓ他在~存里找刎ͼ不会发出sql</p> <p> <wbr></p> <p> <wbr></p> <p><strong>Load</strong><strong>试</strong><strong>:</strong> <strong>开启两?/strong><strong>session</strong><strong>中发Z?/strong><strong>load</strong><strong>查询</strong></p> <p>Student sutdent = (Student)session.load(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p>sessioin.close();</p> <p>………..</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> sutdent = (Student)session.load(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 开启两个session中发Zơload查询Q第一ơload的时候不会去查询数据库,因ؓ他是LAZY的,当用的时候才L询数据库Q?nbsp;<wbr> W二ơload的时候也不会Q当使用的时候查询数据库Q因为session间不能共享一U缓存的数据,因ؓ他会随session的生命周期存在和消亡</p> <p> <wbr></p> <p> <wbr></p> <p><strong>二.</strong><strong>Get</strong><strong>试</strong><strong>:</strong> <strong>在同一?/strong><strong>session</strong><strong>中发Z?/strong><strong>get</strong><strong>查询</strong></p> <p> <wbr> <wbr>  <wbr>Student sutdent = (Student)session.get(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> sutdent = (Student)session.get(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 在同一个session中发Zơget查询Q?W一ơget的时候去查询数据库,W二ơget的时候不会查询数据库Q因Z在缓存里扑ֈQ不会发出sql</p> <p> <wbr></p> <p> <wbr></p> <p><strong>三.</strong><strong>iterate</strong><strong>试</strong><strong>:</strong> <strong>在同一?/strong><strong>session</strong><strong>中发Z?/strong><strong>iterator</strong><strong>查询</strong></p> <p>Student student = (Student)session.createQuery(“from Student s where s.id=1”).iterate().next();</p> <p>System.out.println(student.getName());</p> <p> <wbr></p> <p>student = (Student)session.createQuery(“from Student s where s.id=1”).iterate().next();</p> <p>System.out.println(student.getName());</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 在同一个session中发Zơiterator查询Q第一ơiterate().next()的时候会发出查询id的sqlQ用的时候会发出相应的查询实体对象,W二ơiterate().next()的时候会发出查询id的sqlQ不会发出查询实体对象的sqlQ因为iterate使用~存Q不会发出sql</p> <p> <wbr></p> <p> <wbr></p> <p><strong>四.</strong><strong>Iterate</strong><strong>查询属性测?/strong><strong>:</strong> <strong>同一?/strong><strong>session</strong><strong>中发Zơ查询属?/strong></p> <p>String name = (String)session.createQuery(“select s.name from Student s where s.id=1”).iterate().next();</p> <p>System.out.println(name);</p> <p> <wbr></p> <p>String name = (String)session.createQuery(“select s.name from Student s where s.id=1”).iterate().next();</p> <p>System.out.println(name);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 在同一个session中发Zơ查询属性, W一ơiterate().next()的时候会发出查询属性的sqlQ第二次iterate().next()的时候会发出查询属性的sqlQiterate查询普通属性,一U缓存不会缓存,所以会发出sql</p> <p> <wbr></p> <p> <wbr></p> <strong>五.同一?/strong><strong>session</strong><strong>中先</strong><strong>save</strong><strong>Q再发出</strong><strong>load</strong><strong>查询</strong><strong>save</strong><strong>q的数据</strong> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Student stu = new Student();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> stu.setName(“王五”);</p> <p> <wbr></p> <p> <wbr> <wbr> Serializable id = session.save(stu);</p> <p> <wbr></p> <p>Student sutdent = (Student)session.load(Student.class,id);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></p> <p>save的时候,他会在缓存里放一?不会发出sqlQ因为save是用缓存的</p> <p><strong>六.同一?/strong><strong>session</strong><strong>中先调用</strong><strong>load</strong><strong>查询Q然后执?/strong><strong>sessio.clear()</strong><strong>?/strong><strong>session.evict()</strong><strong>Q再调用</strong><strong>load</strong><strong>查询</strong></p> <p> <wbr></p> <p>Student sutdent = (Student)session.load(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> session.clear();</p> <p> <wbr></p> <p>Student sutdent = (Student)session.load(Student.class,1);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr></p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> sessio.clear()或session.evict()可以理一U缓存,一U缓存无法取消,但可以管?</p> <p>上面的语句都会发出sql 因ؓ一U缓存中的实体被清除?/p> <p><strong> <wbr></strong></p> <p><strong>七.向数据库中批量加?/strong><strong>1000</strong><strong>条数?/strong></p> <p> <wbr></p> <p>for(int i=0;i<1000;i++){</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Student student = new Student();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> student.setName(“s” + i);</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> session.save(student);</p> <p>//?0条数据就强制session数据持久化Q同时清除缓存,避免大量数据造成内存溢出</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> if( i %20 == 0 ){</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> session.flush();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> session.clear();</p> <p>}</p> <p>}</p> <p> </p> <img src ="http://www.aygfsteel.com/lsbwahaha/aggbug/264085.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lsbwahaha/" target="_blank">胡鹏</a> 2009-04-06 11:59 <a href="http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264085.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate查询~存http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264084.html胡鹏胡鹏Mon, 06 Apr 2009 03:58:00 GMThttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264084.htmlhttp://www.aygfsteel.com/lsbwahaha/comments/264084.htmlhttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264084.html#Feedback0http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264084.htmlhttp://www.aygfsteel.com/lsbwahaha/services/trackbacks/264084.html  

hibernate查询~存(hibernate默认是关闭的)

 

查询~存是针Ҏ通属性结果集的缓?/p>

对实体对象的l果集只~存id

 

查询~存的生命周期,当前兌的表发生修改Q那么查询缓存生命周期结?/p>

 

查询~存的配|和使用Q?/p>

1. 启用查询~存Q在hibernate.cfg.xml中加入:

<property name=”hibernate.cache.use_query_cache”>true</property>

  2. 在程序中必须手动启用查询~存Q如Qquery.setCacheable(true);

 

 

试查询~存Q?/strong>

一Q?nbsp; 开启查询缓存,关闭二~存Q开启一?/strong>sessionQ分别调?/strong>query.list  Q查询属性)

 

Query query = session.createQuery(“select s.name from Student s”);

//启用查询~存

query.setCacheable(true);

 

List names = query.list();

for(Iterator iter = names.terator();iter.hasNext();){

       String name = (String)iter.next();

       System.out.println(name);

}

 

System.out.println(“------------------------------------------”);

 

query = session.createQuery(“select s.name from Student s”);

//启用查询~存

query.setCacheable(true);

 

names = query.list();

for(Iterator iter = names.terator();iter.hasNext();){

       String name = (String)iter.next();

       System.out.println(name);

}

W二ơ没有去查询数据库,因ؓ启用了查询缓?/p>

 

二.  开启查询缓存,关闭二~存Q开启两?/strong>sessionQ分别调?/strong>query.list  Q查询属性)

 

Query query = session.createQuery(“select s.name from Student s”);

//启用查询~存

query.setCacheable(true);

 

List names = query.list();

for(Iterator iter = names.terator();iter.hasNext();){

       String name = (String)iter.next();

       System.out.println(name);

}

 

session.close();

 

System.out.println(“------------------------------------------”);

………

Query query = session.createQuery(“select s.name from Student s”);

//启用查询~存

query.setCacheable(true);

 

List names = query.list();

for(Iterator iter = names.terator();iter.hasNext();){

       String name = (String)iter.next();

       System.out.println(name);

}

W二ơ没有去查询数据库,因ؓ查询~存生命周期与session生命周期无关

 

三.  开启查询缓存,关闭二~存Q开启两?/strong>sessionQ分别调?/strong>query.iterate Q查询属性)

 

Query query = session.createQuery(“select s.name from Student s”);

//启用查询~存

query.setCacheable(true);

 

for(Iterator iter =query.iterate();iter.hasNext();){

       String name = (String)iter.next();

       System.out.println(name);

}

 

session.close();

 

System.out.println(“------------------------------------------”);

………

Query query = session.createQuery(“select s.name from Student s”);

//启用查询~存

query.setCacheable(true);

 

for(Iterator iter = query.iterate();iter.hasNext();){

       String name = (String)iter.next();

       System.out.println(name);

}

W二L询数据库Q因为查询缓存只对query.list()起作用,对query.iterate()不v作用,也就是说query.iterate()不用查询缓?/p>

 

四.  关闭查询~存Q关闭二U缓存,开启两?/strong>sessionQ分别调?/strong>query.list Q查询实体对象)

 

Query query = session.createQuery(“ from Student s”);

//query.setCacheable(true);

List students = query.list();

for(Iterator iter = students.iterate();iter.hasNext();){

       Student stu = (Student)iter.next();

       System.out.println(stu.getName());

}

 

session.close();

 

System.out.println(“------------------------------------------”);

………

Query query = session.createQuery(“ from Student s”);

//query.setCacheable(true);

List students = query.list();

for(Iterator iter = students.iterate();iter.hasNext();){

       Student stu = (Student)iter.next();

       System.out.println(stu.getName());

}

W二L询数据库Q因为list默认每次都会发出查询sql

 

五.  开启查询缓存,关闭二~存Q开启两?/strong>sessionQ分别调?/strong>query.list Q查询实体对象)

 

Query query = session.createQuery(“ from Student s”);

query.setCacheable(true);

List students = query.list();

for(Iterator iter = students.iterate();iter.hasNext();){

       Student stu = (Student)iter.next();

       System.out.println(stu.getName());

}

 

session.close();

 

System.out.println(“------------------------------------------”);

………

Query query = session.createQuery(“ from Student s”);

query.setCacheable(true);

List students = query.list();

for(Iterator iter = students.iterate();iter.hasNext();){

       Student stu = (Student)iter.next();

       System.out.println(stu.getName());

W二L询数据库Ӟ会发出N条sql语句Q因为开启了查询~存Q关闭了二~存Q那么查询缓存会~存实体对象的idQ所以hibernate会根据实体对象的idL询相应的实体Q如果缓存中不存在相应的实体Q那么将发出Ҏ实体id查询的sql语句Q否则不会发出sqlQ用缓存中的数?/p>

 

六.  开启查询缓存,开启二U缓存,开启两?/strong>sessionQ分别调?/strong>query.list Q查询实体对象)

 

Query query = session.createQuery(“ from Student s”);

query.setCacheable(true);

List students = query.list();

for(Iterator iter = students.iterate();iter.hasNext();){

       Student stu = (Student)iter.next();

       System.out.println(stu.getName());

}

 

session.close();

 

System.out.println(“------------------------------------------”);

………

Query query = session.createQuery(“ from Student s”);

query.setCacheable(true);

List students = query.list();

for(Iterator iter = students.iterate();iter.hasNext();){

       Student stu = (Student)iter.next();

       System.out.println(stu.getName());

}

 

W二不会发出sqlQ因为开启了二~存和查询缓存,查询~存~存了实体对象的id列表Qhibernate会根据实体对象的id列表CU缓存中取得相应的数?/p>

胡鹏 2009-04-06 11:58 发表评论
]]>
hiernate抓取{略和批量策?/title><link>http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264081.html</link><dc:creator>胡鹏</dc:creator><author>胡鹏</author><pubDate>Mon, 06 Apr 2009 03:57:00 GMT</pubDate><guid>http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264081.html</guid><wfw:comment>http://www.aygfsteel.com/lsbwahaha/comments/264081.html</wfw:comment><comments>http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264081.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264081.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lsbwahaha/services/trackbacks/264081.html</trackback:ping><description><![CDATA[<p><strong>?/strong><strong>hibernate</strong><strong>中设|:</strong></p> <p><strong>查询数据库每ơ最多返?/strong><strong>50</strong><strong>条结果:</strong></p> <p><strong><property name=”hibernate.jdbc.fetch_size”>50</property></strong></p> <p><strong>30</strong><strong>条更新数据库一?/strong><strong>:</strong></p> <p><strong><property name= ”hibernatejdbc.batch_size”>30</property></strong></p> <p><strong> <wbr></strong><strong>但不是所有的数据库都支持的,</strong><strong>sqlServer</strong> <strong>?/strong><strong>orcale</strong> <strong>都支持的?/strong></p> <p><strong> <wbr></strong></p> <p><strong>一Q?/strong><strong>hibernate</strong> <strong>抓取{略</strong><strong>(Fetch)</strong> <strong>单端代理扚w抓取</strong></p> <p> <wbr></p> <p><strong>1.fetch=”select”</strong> <strong>兌实体</strong></p> <p>//fetch 默认是select</p> <p><many-to-one name="businessId" column="business_id" insert="true" update="true" fetch="select"></p> <p> <wbr></p> <p>Student student = (Student)session.load(Student.class,1);</p> <p>System.out.println(student.getName());</p> <p>System.out.println(student.getClasses().getName()); //多对一中的属性班U,获取班名称</p> <p> <wbr></p> <p>Fetch=”select” ,上面E序会发2条sql语句Q第二条发送一条select语句抓取当前对象兌实体或集?(q里指是班名称)</p> <p> <wbr></p> <p><strong>2.fetch=” join”</strong> <strong>兌实体</strong></p> <p>//fetch 讄?join</p> <p><many-to-one name="businessId" column="business_id" insert="true" update="true"</p> <p>fetch=" join "></p> <p> <wbr></p> <p>Student student = (Student)session.load(Student.class,1);</p> <p>System.out.println(student.getName());</p> <p>System.out.println(student.getClasses().getName()); //多对一中的属性班U,获取班名称</p> <p> <wbr></p> <p>fetch=” join” , 上面E序会发1条sql语句Q?hibernate会通过select使用外链接来加蝲其关联实体或集合Q此时lazy会失?nbsp;<wbr></p> <p> <wbr></p> <p><strong>二.</strong><strong>hibernate</strong> <strong>抓取{略</strong><strong>(Fetch)</strong> <strong>集合代理扚w抓取</strong></p> <p> <wbr></p> <p><strong>1. <wbr> <wbr> <wbr> <wbr></strong> <strong>fetch=”select”</strong></p> <p>//fetch 默认是select</p> <p align="left"><set name="students" inverse="true" cascade="all" fetch="select"></p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <key column="classid" /></p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <one-to-many class="com.Student" /></p> <p></set></p> <p> <wbr></p> <p>Classes cla = (Classes)session.load(Classes.class,1);</p> <p>System.out.println(cla.getName());</p> <p>for(Iterator iter = cla.getStudents().iterator();iter.hasNext();){</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Student student = (Student)iter.next();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p>}</p> <p> <wbr></p> <p>fetch=”select” ,上面E序用到了就发sql语句,W二从@环中发了N?如果Qfetch=”subselect”,则只是发送一条语句,见下?/p> <p> <wbr></p> <p><strong>2 fetch=”join”</strong></p> <p>//fetch 讄成join</p> <p align="left"><set name="students" inverse="true" cascade="all" fetch="join"></p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <key column="classid" /></p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <one-to-many class="com.Student" /></p> <p></set></p> <p> <wbr></p> <p>Classes cla = (Classes)session.load(Classes.class,1);</p> <p>System.out.println(cla.getName());</p> <p>for(Iterator iter = cla.getStudents().iterator();iter.hasNext();){</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Student student = (Student)iter.next();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p>}</p> <p> <wbr></p> <p>Fetch=”select” ,上面E序只发了一条sql语句</p> <p> <wbr></p> <p> <wbr></p> <p><strong>三.</strong><strong>hibernate</strong> <strong>抓取{略</strong><strong>(Fetch)</strong> <strong>集合代理扚w抓取</strong></p> <p> <wbr></p> <p><strong>2. <wbr> <wbr> <wbr> <wbr></strong> <strong>fetch=”subselect”</strong></p> <p>//fetch讄成subselect</p> <p align="left"><set name="students" inverse="true" cascade="all" fetch="subselect"></p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <key column="classid" /></p> <p align="left"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <one-to-many class="com.Student" /></p> <p></set></p> <p> <wbr></p> <p>List classesList = session.createQuery(“select c from Classes c where c.id in(1,2,3)”);</p> <p>for(Iterator iter=classesList.iterator();iter.hasNext();){</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Classess classess = (Classess)iter.next();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(“classes.name=”+ classes.getName());</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> for(Iterator iter1 = classess.getStudents().iterator();iter1.hasNext();){</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Student student = (Student)iter1.next();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p>}</p> <p>}</p> <p> <wbr></p> <p>Fetch=”subselct” ,另外发送一条select语句抓取在前面查询到的所有实体对象的兌集合</p> <p> <wbr></p> <p> <wbr></p> <p><strong>四.</strong><strong>hibernate</strong> <strong>扚w{略</strong><strong>batch-size</strong><strong>属性,可以扚w加蝲实体c,</strong></p> <p><strong>参见</strong><strong>Classes.hbm.xml</strong><strong>Q同旉合也可以是用,如:</strong></p> <p><strong> <wbr></strong><set name="students" inverse="true" cascade="save-update" batch-size=”5”></p> <p><strong> <wbr></strong></p> <p>在配|文件hbm.xml 讄中:</p> <p><class name="com.Classes " table="tb_classes" batch-size=”3”></p> <p> <wbr></p> <p>List students = session.createQuery(“select s from Student s where s.id in(:ids)”).setParameterList(“:ids”,new Object[]{1,11,21,31,41,51,61,71,81,91}).list();</p> <p> <wbr></p> <p>for(Iterator iter=students.iterator();iter.hasNext();){</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> Student student = (Sutdent)iter.next();</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getName());</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> System.out.println(student.getClassess().getName());</p> <p> <wbr></p> <p>}</p> <p> <wbr></p> <p>当第二个for循环Ӟ每次加蝲 之前讄?数量 实体对象, <wbr> 如:现在讄3Q那么当Iterator iter=students.iterator();iter.hasNext();时候,他会查询?个student对象</p> <p><wbr></p> <img src ="http://www.aygfsteel.com/lsbwahaha/aggbug/264081.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lsbwahaha/" target="_blank">胡鹏</a> 2009-04-06 11:57 <a href="http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264081.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate悲观?与乐观锁http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264082.html胡鹏胡鹏Mon, 06 Apr 2009 03:57:00 GMThttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264082.htmlhttp://www.aygfsteel.com/lsbwahaha/comments/264082.htmlhttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264082.html#Feedback0http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264082.htmlhttp://www.aygfsteel.com/lsbwahaha/services/trackbacks/264082.html悲观?/strong>

      

       悲观锁的实现Q通常依赖于数据库机制Q在整个q程中将数据锁定Q其它Q何用户都不能d或修攏V?/p>

 

举个例子Q?/p>

//查询id=1的那条记录,使用悲观?/p>

User user = (User)session.load(User.class, 1 ,  LockMode.UPGRADE);

使用了数据库中的 for update  锁定

 

乐观?/strong>

大多数基于数据版本记录机ӞversionQ实玎ͼ一半是在数据库表加入一个version字段Q读取数据时版本号一同读出,之后更新数据时版本号加一Q如果提交数据时版本号小于或{于数据库表中的版本P则认为数据是q期的,否则l予更新?/p>

 

其实q是锁Q是一U冲H检?nbsp; (没有hibernate也可以用的Q自p在数据库中徏字段来控Ӟ使用hibernate方便些,装好了)

 

?.hm.xml?dU色部分Q?/p>

                                            

<class

name="com.tao3c.orm.TbBusinessInfo"  table="tb_business_info"  optimistic-lock=”version”>

 

其中version是com.tao3c.orm.TbBusinessInfocȝ属性,hiernbate会去l护的,不用自己去该

 

 



胡鹏 2009-04-06 11:57 发表评论
]]>
hibernate悲观?与乐观锁http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264083.html胡鹏胡鹏Mon, 06 Apr 2009 03:57:00 GMThttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264083.htmlhttp://www.aygfsteel.com/lsbwahaha/comments/264083.htmlhttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264083.html#Feedback0http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264083.htmlhttp://www.aygfsteel.com/lsbwahaha/services/trackbacks/264083.html悲观?/strong>

      

       悲观锁的实现Q通常依赖于数据库机制Q在整个q程中将数据锁定Q其它Q何用户都不能d或修攏V?/p>

 

举个例子Q?/p>

//查询id=1的那条记录,使用悲观?/p>

User user = (User)session.load(User.class, 1 ,  LockMode.UPGRADE);

使用了数据库中的 for update  锁定

 

乐观?/strong>

大多数基于数据版本记录机ӞversionQ实玎ͼ一半是在数据库表加入一个version字段Q读取数据时版本号一同读出,之后更新数据时版本号加一Q如果提交数据时版本号小于或{于数据库表中的版本P则认为数据是q期的,否则l予更新?/p>

 

其实q是锁Q是一U冲H检?nbsp; (没有hibernate也可以用的Q自p在数据库中徏字段来控Ӟ使用hibernate方便些,装好了)

 

?.hm.xml?dU色部分Q?/p>

                                            

<class

name="com.tao3c.orm.TbBusinessInfo"  table="tb_business_info"  optimistic-lock=”version”>

 

其中version是com.tao3c.orm.TbBusinessInfocȝ属性,hiernbate会去l护的,不用自己去该

 

 



胡鹏 2009-04-06 11:57 发表评论
]]>
hibernate多对多映_处理http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264079.html胡鹏胡鹏Mon, 06 Apr 2009 03:56:00 GMThttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264079.htmlhttp://www.aygfsteel.com/lsbwahaha/comments/264079.htmlhttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264079.html#Feedback0http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264079.htmlhttp://www.aygfsteel.com/lsbwahaha/services/trackbacks/264079.html一Q?/strong>hibernate多对多映(单项Q,自动产生W三张表

User--à Role

user(id,name)    role(id,name)

Userc:

private int  id;   private String name;  private  Set roles;

 

Rolec:

private int id;    private String name;

 

User.hbm.xml中:

<set name=”roles” table=”t_user_role”>

       <key column=”userid” />

       <many-to-many class=”com.Role” column=”roleid” />

</set>

 

q样Qhibernate会自动生中间表t_user_role(userid,roleid) q且是复合主键,userid为t_user的外键,roleid为t_role的外?/p>

 

 

二.hibernate多对多的存储

 

Role r1 = new Role();

r1.setName(“数据录入人员”);

 

Role r2 = new Role();

r2.setName(“商务ȝ”);

 

Role r3 = new Role();

r3.setName(“大区l理”);

 

 

User u1 = new User();

u1.setName(“10”);

Set u1Roles = new HashSet();

u1Roles.add(r1);

u1Roles.add(r2);

u1.setRoles(u1Roles);

 

User u2 = new User();

u1.setName(“儿”);

Set u2Roles = new HashSet();

u2Roles.add(r2);

u2Roles.add(r3);

u2.setRoles(u2Roles);

User u3 = new User();

u3.setName(“Cu”);

Set u3Roles = new HashSet();

u3Roles.add(r1);

u3Roles.add(r2);

u3Roles.add(r3);

u3.setRoles(u3Roles);

 

 

session.save(r1);

session.save(r2);

session.save(r3);

 

session.save(u1);

session.save(u2);

session.save(u3);

 

l束之后Q在W三张表中也有数?/p>

 

 

 

三.hibernate多对多的加蝲

 

User user = (User)session.load(User.class,1);

System.out.println(user.getName());

for(Iterator iter = user.getRoles().iterator();iter.hasNext();){

       Role role = (Role)iter.next();

       System.out.println(role.getName());

 

}

 每次加蝲都会发出sql语句

 

三.hibernate多对多的 双向映射

 

User ß--.-àRole

user(id,name)    role(id,name)

Userc:

private int  id;   private String name;  private Set roles;

 

Rolec:

private int id;    private String name; private Set users;

 

User.hbm.xml中:

<set name=”roles” table=”t_user_role”>

       <key column=”userid” />

       <many-to-many class=”com.Role” column=”roleid” />

</set>

 

Role.hbm.xml中:

<set name=”roles” table=”t_user_role”>   //q里可以讄order-by=”userid”,Ҏuserid排序

       <key column=”roleid” />

       <many-to-many class=”com.User” column=”userid” />

</set>

 

注意Q上面中间表名字是一L

q样Qhibernate也会自动产生中间表t_user_role(userid,roleid) q且是复合主键,userid为t_user的外键,roleid为t_role的外?/p>

 



胡鹏 2009-04-06 11:56 发表评论
]]>
hibernate多对多映_处理http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264080.html胡鹏胡鹏Mon, 06 Apr 2009 03:56:00 GMThttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264080.htmlhttp://www.aygfsteel.com/lsbwahaha/comments/264080.htmlhttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264080.html#Feedback0http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264080.htmlhttp://www.aygfsteel.com/lsbwahaha/services/trackbacks/264080.html一Q?/strong>hibernate多对多映(单项Q,自动产生W三张表

User--à Role

user(id,name)    role(id,name)

Userc:

private int  id;   private String name;  private  Set roles;

 

Rolec:

private int id;    private String name;

 

User.hbm.xml中:

<set name=”roles” table=”t_user_role”>

       <key column=”userid” />

       <many-to-many class=”com.Role” column=”roleid” />

</set>

 

q样Qhibernate会自动生中间表t_user_role(userid,roleid) q且是复合主键,userid为t_user的外键,roleid为t_role的外?/p>

 

 

二.hibernate多对多的存储

 

Role r1 = new Role();

r1.setName(“数据录入人员”);

 

Role r2 = new Role();

r2.setName(“商务ȝ”);

 

Role r3 = new Role();

r3.setName(“大区l理”);

 

 

User u1 = new User();

u1.setName(“10”);

Set u1Roles = new HashSet();

u1Roles.add(r1);

u1Roles.add(r2);

u1.setRoles(u1Roles);

 

User u2 = new User();

u1.setName(“儿”);

Set u2Roles = new HashSet();

u2Roles.add(r2);

u2Roles.add(r3);

u2.setRoles(u2Roles);

User u3 = new User();

u3.setName(“Cu”);

Set u3Roles = new HashSet();

u3Roles.add(r1);

u3Roles.add(r2);

u3Roles.add(r3);

u3.setRoles(u3Roles);

 

 

session.save(r1);

session.save(r2);

session.save(r3);

 

session.save(u1);

session.save(u2);

session.save(u3);

 

l束之后Q在W三张表中也有数?/p>

 

 

 

三.hibernate多对多的加蝲

 

User user = (User)session.load(User.class,1);

System.out.println(user.getName());

for(Iterator iter = user.getRoles().iterator();iter.hasNext();){

       Role role = (Role)iter.next();

       System.out.println(role.getName());

 

}

 每次加蝲都会发出sql语句

 

三.hibernate多对多的 双向映射

 

User ß--.-àRole

user(id,name)    role(id,name)

Userc:

private int  id;   private String name;  private Set roles;

 

Rolec:

private int id;    private String name; private Set users;

 

User.hbm.xml中:

<set name=”roles” table=”t_user_role”>

       <key column=”userid” />

       <many-to-many class=”com.Role” column=”roleid” />

</set>

 

Role.hbm.xml中:

<set name=”roles” table=”t_user_role”>   //q里可以讄order-by=”userid”,Ҏuserid排序

       <key column=”roleid” />

       <many-to-many class=”com.User” column=”userid” />

</set>

 

注意Q上面中间表名字是一L

q样Qhibernate也会自动产生中间表t_user_role(userid,roleid) q且是复合主键,userid为t_user的外键,roleid为t_role的外?/p>

 



胡鹏 2009-04-06 11:56 发表评论
]]>
hibernate配置要点详谈http://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264076.html胡鹏胡鹏Mon, 06 Apr 2009 03:53:00 GMThttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264076.htmlhttp://www.aygfsteel.com/lsbwahaha/comments/264076.htmlhttp://www.aygfsteel.com/lsbwahaha/archive/2009/04/06/264076.html#Feedback0http://www.aygfsteel.com/lsbwahaha/comments/commentRss/264076.htmlhttp://www.aygfsteel.com/lsbwahaha/services/trackbacks/264076.html    

1.两种配置文gQ?/p>

A.hibernate.cfg.xml和B.hibernate.properties

 

A中可含映文件的配置Q而B中hard codes加映文件?/p>

 

A.Configuration config=new Configuration().config();

B. Configuration config=new Configuration();

config.addClass(TUser.class);

 

2.你不必一定用hibernate.cfg.xml或hibernate.propertiesq两文g名,你也不一定非得把配置文g攑֜Classes下, File file=new File("c:\\sample\\myhibernate.xml"); Configuration config=new Configuration().config(file);

 

3. session.Flush() 强制数据库立卛_步,当用事务Ӟ不必用flush,事务提交自动调用flush在session关闭时也会调用flush

 

4. HibernateL使用对象cd作ؓ字段cd

 

5. XDoclet专门建立了hibernate doclet,是在java代码上加上一些java docTagQ后来再让XDoclet分析该java代码Q生成映文?

 

6.HQL子句本n大小写无养I但是其中出现的类名和属性名必须注意大小写区分?/p>

 

7.关系Q Constrained : U束Q表明主控表的主键上是否存在一个外键(foreigh keyQ对其进行约束?/p>

 

property-ref:兌cM用于与主控类相关联的属性名Q默认ؓ兌cȝ主键属性名

 

单向一对多需在一斚w|,双向一对多需在双方进行配|?/p>

 

8.lazy=false:被动方的记录由hibernate负责记取Q之后存攑֜LҎ定的Collectioncd属性中

 

9. java.util.Set或net.sof.hibernate.collecton.Bagcd的Collection

 

10.重要Qinverse:用于标识双向兌中的被动方一端?/p>

 

inverse=false的一方(L方)负责l护兌关系.默认|false

 

11.batch-size:采用延迟加蝲特征Ӟ一ơ读入的数据数昨?/p>

 

12.一对多通过LҎ斎ͼL方ؓ一ҎQ?/p>

 

user.getAddresses().add(addr);

 

session.save(user);//通过L对象U联更新

 

13.在one-to-many 关系中,many 一方设Z动方Qinverse=falseQ将有助性能的改善。在一方设|关pLQinverse=true,卛_L权交l多方, q样多方可主动从一方获得foreign key,然后一ơinsert卛_完工?/p>

 

addr.setUser(user);//讄兌的TUser对象

 

user.getAddresses().add(addr);

 

session.save(user);//U联更新

 

14.只有设ؓL方的一Ҏ兛_Q访问)Ҏ的属性,被动Ҏ不关心对方的属性的?/p>

 

15.one-to-many与many-to-one节点的配|属性不同:

 

一对多关系多了lazy和inverse两个属性多对多节点属性:

 

column:中间映射表中Q关联目标表的关联字D?/p>

 

class:cdQ关联目标类

 

outer-join:是否使用外联?/p>

 

注意:access是设|属性值的d方式?/p>

 

column是设|关联字Dc?/p>

 

16.多对多,注意两方都要讄inverse和lazy,cascade只能设ؓinsert-update

 

多对多关pMQ由于关联关pL两张表相互引用,因此在保存关pȝ态时必须对双方同时保存?/p>

 

group1.getRoles().add(role1); role1.getGroups().add(group1);

 

session.save(role1); session.save(group1);

 

17.关于vo和po vol过hibernate定w处理Q就变成了po(该vo的引用将被容器保存,q且在session关闭时flush,因此po如果再传到其它地Ҏ变了Q就危险? vo和po怺转换QBeanUtils.copyProperties(anotherUser,user);

 

18.对于save操作而言Q如果对象已l与Session相关联(卛_l被加入Session的实体容器中Q,则无需q行具体的操作。因Z后的Session.flushq程中,Hibernate 会对此实体容器中的对象进行遍历,查找出发生变化的实体Q生成ƈ执行相应的update 语句?/p>

 

19.如果我们采用了gq加载机Ӟ但希望在一些情况下Q实现非延迟加蝲时的功能Q也是_我们希望在Session关闭后,依然允许操作user的addresses 属?Hibernate.initializeҎ可以通过强制加蝲兌对象实现q一功能Q?q也正是我们Z么在~写POJOӞ必须用JDK Collection接口Q如Set,MapQ? 而非特定的JDK Collection实现c(如HashSet、HashMapQ申明Collection属性的 原因?/p>

 

20.事务Q从sessionFactory获得session,其自动提交属性就已经关闭(AutoCommit=false),此时若执行了jdbc操作Q如果不昑ּ调用session.BeginTransaction(),是不会执行事务操作的?/p>

 

jdbc transaction:Z同一个session(是同一个connection)的事?

 

jta transaction:跨sessionQ跨connectionQ事?

 

对于jta事务Q有三种实现ҎQ?/p>

 

A。UserTransaction tx=new InitialContext().lookup("...");

tx.commit();

 

B. 使用hibernate装的方法:(不推?

 

Transaction tx=session.beginTransaction();

tx.commit();

C. 使用ejb之sessionBean的事务技持方法,你只要在把需要在发布描述W中Q把需要jta事务的方法声明ؓrequire卛_

 

21.悲观锁,乐观锁: 乐观锁一般通过version来实玎ͼ注意version节点必须出现在id后?/p>

 

22.Hibernate中,可以通过Criteria.setFirstResult和Criteria.setFetchSizeҎ讑֮分页范围?/p>

 

Query接口中也提供了与其一致的ҎQhibernate主要在dialectcM实现在这个功能?/p>

 

23.cache

 

……

 

net.sf.ehcache.hibernate.Provider 

 

q需对ecache本nq配|?/p>

 

 

 

之后在映文件中指定各个映射实体的cache{略

 

 

....

 

 

....

 

 

***************************************************** Query.list()跟Query.iterate()的不同:对于query.list()L通过一条sql语句获取所有记?然后其dQ填入pojoq回; 但是query.iterate()Q则是首先通过一条Select SQL 获取所有符合查询条件的记录?idQ再对这个id 集合q行循环操作Q通过单独的Select SQL 取出每个id 所对应的记 录,之后填入POJO中返回?/p>

 

也就是说Q对于list 操作Q需要一条SQL 完成。而对于iterate 操作Q需要n+1 条SQL。,listҎ不会从Cache中读取数据。iterator却会?/p>

 

24.ThreadLocal:它会为每个线E维护一个私有的变量I间。实际上Q?其实现原理是在JVM 中维护一个MapQ这个Map的key 是当前的线E对象,而value则是U程通过ThreadLocal.setҎ保存的对象实例。当U程调用ThreadLocal.getҎӞ ThreadLocal会根据当前线E对象的引用Q取出Map中对应的对象q回?/p>

 

q样QThreadLocal通过以各个线E对象的引用作ؓ区分Q从而将不同U程的变量隔d来?/p>

 

25.Hibernate官方开发手册标准示?

 

public class HibernateUtil { private static SessionFactory sessionFactory;

static { try { // Create the SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (HibernateException ex) { throw new RuntimeException( "Configuration problem: " + ex.getMessage(), ex );

} } public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException { Session s = (Session) session.get();

// Open a new Session, if this Thread has none yet if (s == null) { s = sessionFactory.openSession();

session.set(s);

} return s;

} public static void closeSession() throws HibernateException { Session s = (Session) session.get();

session.set(null);

if (s != null) s.close();

} }

 

26.通过filter实现session的重用:

 

public class PersistenceFilter implements Filter { protected static ThreadLocal hibernateHolder = new ThreadLocal();

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { hibernateHolder.set(getSession());

try { …… chain.doFilter(request, response);

…… } finally { Session sess = (Session)hibernateHolder.get();

if (sess != null) { hibernateHolder.set(null);

try { sess.close(); } catch (HibernateException ex) { throw new ServletException(ex);

} } } } ……}



胡鹏 2009-04-06 11:53 发表评论
]]>
վ֩ģ壺 | ˫| Ƥ| ̩| | | | ƶ| | ݳ| ʯ| γ| | ɽ| | ̳| ̶| | | Ӣ| | | | گ| | ˳| Ž| | | | ¡| ϻ| | ɳ| ī| | ϽϽ| ̨| Ӫɽ| | ͨ|