Hibernate知識點總結

來源:互聯網
上載者:User

標籤: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.構造器注入



































































聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.