码迷,mamicode.com
首页 > 系统相关 > 详细

Hibernate知识点总结

时间:2014-06-26 07:24:37      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:hibernate   spring   

Hibernate配置二级缓存:  --- 使用EhCache


1.hibernate.cfg.xml中配置二级缓存


<hibernate-configuration>
  <session-factory>
<!-- 使用EHCache配置Hibernate二级缓存 -->
    <property name="hibernate.cache.user_second_level_cache">true</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
  </session-factory>
</hibernate-configuration>  


2.在持久化类的映射文件中需要指定缓存的同步策略,关键代码:


   --- 产品信息字段配置信息
<hibernate-mapping>
<class name="com.mr.product.Product" table="tab+product">
<cache usage="read-only">                           //指定缓存的同步策略
</hibernate-mapping>


3.在项目的classpath根目录下加入换成配置文件ehcache.xml,该文件可一直hibernate的zip包下的etc目录中找到。
  缓存配置文件代码如下:


<ehcache>
<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
</ehcache>




  Hibernate的映射关系


双向多对一关联:product  n <---> 1  factory


既可以通过主控方加载被控方,也可以通过被控方加载主控方
factory配置:
       //定义一对多映射
        <set name="products" 
          inverse="true">
          <key column="factoryid"/>
          <one-to-many class="com.mr.product.Product"/>
        </set>


product配置:
      //定义多对一映射
        <many-to-one name="factory" class="com.mr.factory.Factory">
        <column name="factoryid">
        </many-to-one>




一对一外键关联  --- 实际上就是多对一关联的一个特例而已,需要保证关联字段的唯一性,
                                        在<many-to-one>元素中通过 unique属性就可实现关联字段的唯一性


配置一对一关联: People <---> IDcard
               
People映射配置:一对一映射
            <many-to-one name="idcard" unique="true">
               <column name="card_id"/>
            </many-to-one>






多对多关联: --- 通过中间表             user          user-role            role
                        
                                                                id                 id                         id
                                                                name         user_id               rolename
                                                                                   role_id




User映射:


       <set name="roles" table="table_user_role">
                 <key column="user_id"></key>
                 <many-to-many class="com.mr.role.Role" column="role_id"/>
       </set>
       
Role映射:


       <set name="users" table="table_user_role">
                 <key column="role_id"></key>
                 <many-to-many class="com.mr.user.User" column="user_id"/>
       </set>








级联操作:  --  当主控方执行save、update、delete操作时,管理对象(被控方)是否进行同步操作,
                 在映射文件中通过对 cascade属性的设置决定是否对关联对象采用级联操作。
      
      cascade级联操作参数设置:
      
      all                         所有情况均采用级联操作
      none                        默认参数,所有情况下均不采用级联操作
      save-update                 在执行save-update方法时执行级联操作
      delete                      在执行delete方法时执行级联操作
    
    eg:对于People   --   设置级联删除,当删除People对象的时候,会级联删除关联的IDcard对象,即delete People时,会先有一条select的SQL,再有两条delet的SQL
       <one-to-one name="idcard" calss="com.mr.idcard.IDcard" cascade="delete"></one-to-one>  




HQL查询:


      语法:
      select     对象.属性名
      from       对象
      where      过滤条件
      group by   对象.属性名 
      having     分组条件
      order by   对象.属性名


实体对象查询
   from 对象 对象别名 where 条件
   eg:
   Query q = session.createQuery("from Employee emp");
   emplist = q.list();




HQL参数绑定机制: 
   
   1.绑定?占位符
   eg:
   Query q = session.createQuery("from Employee emp where sex =?");
   q.setParameter(0,"男");
   emplist = q.list();


   1.绑定:parameter占位符
   eg:
   Query q = session.createQuery("from Employee emp where sex =:sex");
   q.setParameter("sex","男");
   emplist = q.list();








Spring :
 
 1.使用BeanFactory管理bean ,在getBean方法之前,不会实例化对象
   eg:装载bean:
     Resource resource = new ClassPathResource("applicationContext.xml");  //装载配置文件
     BeanFactory factory = new XmlBeanFactory(resource);
     Test test = (Test)factory.getBean("test");
     
 2.ApplicationContext的应用:
   ApplicationContext扩展了BeanFactory的功能,添加了如I18n,生命周期时间的发布监听等更强大的功能
   
   ApplicationContext接口有3个实现类,可以实例化其中任何一个类来创建Spring的ApplicationContext容器。
      
      1.ClassPathXmlApplicationContext  -- 从当前类路径中检索配置文件并装载它来创建容器的实例:
        eg:  ApplicationContext context = new ClassPathXmlApplicationContext(String configLocation);
        
      2.FileSystemXmlApplicationContext -- 通过参数指定配置文件的位置,可以获取类路径之外的资源。
        eg: ApplicationContext context = FileSystemXmlApplicationContext(String configLocation);
     
      3.WebApplicationContext           -- Spring的Web应用容器,有两种方法可以在Servlet中使用WebApplicationContext
         1.在Servlet的web.xml中配置Sping的ContextLoaderListener监听器
         2.web.xml中,添加一个Servlet,使用Spring的org.springframework.web.context.ContextLoaderServlet类
         
 依赖注入的三种类型:
    1.接口注入
    2.setter注入
    3.构造器注入



































































Hibernate知识点总结,布布扣,bubuko.com

Hibernate知识点总结

标签:hibernate   spring   

原文地址:http://blog.csdn.net/he90227/article/details/34450095

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!