Hibernate对象关系映射总结
2.sessionFactory级别的缓存,需要手动配置;所有的session可以共享sessionFactory 级别的缓存;(一般把一些不经常变
化的实体对象放到sessionFactory级别的缓存中,适合放不经常变化的实体对象。)
3.Hiberante3二级缓存的配置和使用方法如下:
1:hibernate.cfg.xml 文件中配置如下:打开二级缓存:二级缓存的提供者:(依懒包:ehcache-1.2.3.jar) <property name="http://www.77cn.com.cne_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvidehcache-1.2.3.jarer</property> <property name="http://www.77cn.com.cne_query_cache">true</property> 打开用户缓存 2:src 下需要一个配置文件:ehcache.xml 3:lib下加包:ehcache-1.2.3.jar
4:将要查询缓存的对象:在hibernate.cfg.xml 中进行配置(JPA方式) <mapping class="http://www.77cn.com.cn.hibernateTest.ehcache.Category"/> <mapping class="http://www.77cn.com.cn.hibernateTest.ehcache.Msg"/> <mapping class="http://www.77cn.com.cn.hibernateTest.ehcache.Topic"/> (xml方式)
将要查询缓存的对象:在映射文件中进行配置 <class name="Department" table="oneToManyDupDir_Department"> <cache usage="read-write"/> 一定要写在主键定义之前 <id name="id"> <generator class="native" /> </id> <property name="name"/> </class>
===================================list与iterator区别================================= 1:List 仅仅会填充二级缓存,却不能利用二级缓存:当打开查询缓存时:list能利用查询缓存。 List 是一次读出所有的数据:
2:iterator可以读二级缓存,先从缓存中取数据:没有数据时iterator首先读出所有的id.等用到时才会根据id发送sql语句从 数据库读数据.
测试list:一级缓存:当打开查询缓存时:list也能利用查询缓存*/
public void testQueryList() { Session session = sf.openSession(); session.beginTransaction(); List<Category> categories = (List<Category>)session.createQuery("from Category").list(); for(Category c : categories) {
System.out.println(c.getName()); } 对于list没法利用session级别的缓存(不知道到下次查询的条件是否一样):
还会去数据库取并且刷该缓存(将原来的对象刷新一遍)*/
List<Category> categories2 = (List<Category>)session.createQuery("from Category").list(); for(Category c : categories2) {
System.out.println(c.getName()); }
session.getTransaction().commit(); session.close();