hibernate 筆記一

來源:互聯網
上載者:User

初學hibernate,配置完後運行出現

1、hibernate.dialect' must be set when no Connection available錯誤;

檢查設定檔,寫的很對就是不知道哪出錯了,後來度娘告訴我:

Configuration cfg = new Configuration();這樣寫預設讀取的是hibernate.properties設定檔,而我只是添加了,hibernate.cfg.xml設定檔,所以應這麼寫:

Configuration cfg = new Configuration().configure();

2、hibernate二級緩衝配置。

  hibernate二級緩衝是由SessionFactory管理,所以又叫SessionFactory級緩衝,它是通過不同的類庫來實現 的,比如ehcache、oscache等。和一級緩衝一樣,二級緩衝也是用來緩衝實體物件的,對普通屬性不緩衝。

  

hibernate二級緩衝的使用需要進行必要的配置,主要是四個地方(這裡以ehcache為例):

  1>. 配置echcache.xml檔案

  2>.開啟二級緩衝,修改hibernate.cfg.xml檔案

true

  3>.指定緩衝產品供應商,修改hibernate.cfg.xml檔案

org.hibernate.cache.EhCacheProvider

  4>.指定那些實體類使用二級緩衝(兩種方法)

  1).在對應檔中採用標籤

  2).在hibernate.cfg.xml檔案中,採用標籤

  hibernate二級緩衝配置上之後,就成了“客觀存在”,hibernate在使用某些方法的時候預設就使用和維護了二級緩衝(哪怕你出於 某種原因希望使用也不行)。因此,在使用二級緩衝時進行一定的控制還是必要的,Session就提供了設定使用二級緩衝的模式的方法 (setCacheMode)來實現,當session調用某個方法時對二級緩衝的存取改變。

   1.實體類:   

    Student.java 
public class Student { private Integer id; private String name; //一系列的setter.getter方法 }

  

2.對應檔:

  Student.hbm.xml

<class name="com.sxt.hibernate.cache.entity.Student" table="sxt_hibernate_student">  <!-- 指定本類的對象使用二級緩衝(這也可以放在hibernate.cfg.xml中統一指 定) -->  <!--  <cache usage="read-only"/>  -->  <id name="id" length="4">  <generator class="native"></generator>  </id>  <property name="name" length="10"></property>  </class> 

3. 二級緩衝設定檔:

  ehcache.xml

<ehcache>  <!-- 當二級緩衝溢出時,對象儲存的臨時磁碟路徑 -->  <diskStore path="java.io.tmpdir"/>  <!--name="sampleCache2" 緩衝名字  maxElementsInMemory="1000" 緩 存裡可存放的最大對象數.  eternal="true" 緩衝對象是否永久有效(true表示是).  timeToIdleSeconds="120" 對 象在緩衝中存活的空閑時間,即空閑多久它就失效,單位是秒.  timeToLiveSeconds="120" 對 象在緩衝中存活的時間,單位是秒.  overflowToDisk="true"    當緩衝溢出時,對象 是否儲存到磁碟上.儲存的磁碟路徑由<diskStore>中的path指定.  -->  <defaultCache  maxElementsInMemory="10000"  eternal="false"  timeToIdleSeconds="120"  timeToLiveSeconds="120"  overflowToDisk="true"  />  </ehcache> 

4.hibernate設定檔

  hibernate.cfg.xml

<hibernate-configuration>  <session-factory>  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL10</property>  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  <property name="hibernate.connection.username">scott</property>  <property name="hibernate.connection.password">yf123</property>  <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>  <property name="hibernate.show_sql">true</property>  <!-- 開啟二級緩衝,其實hibernate預設就是開啟的,這裡顯示的指定一下 -->  <property name="hibernate.cache.use_second_level_cache">true</property>  <!-- 指定二級緩衝產品的供應商 -->  <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>  <mapping resource="com/sxt/hibernate/cache/entity/Student.hbm.xml"/>  <!-- 指定那些類使用二級緩衝 -->  <class-cache usage="read-only" class="com.sxt.hibernate.cache.entity.Student"/>  </session-factory>  </hibernate-configuration>

  

在對應檔中配置:<cache usage="read-only"/>

注意:在這有4個值:有必要對這4個值進行介紹。

A.transactional:儲存可重複的事物隔離等級,對於讀/寫比例大,很少更新的資料通常可以採用這種方式。
B.read-write:使用timestamp機制維護已提交的事物隔離等級,對於讀/寫比例大,很少更新的資料通常可以採用這種方式。
C.nonstrict-read-write:二級緩衝與資料庫中的資料可能會出現不一致的情況。應該設定足夠短的緩衝到期時間。否則就有可能從緩衝中讀取到髒資料,當一些資料很少改變(一天,兩天都不改變的資料)並且這些資料如果出現資料庫與緩衝不一致的情況下影響並不大的時候,那麼可以採用這種緩衝策略。

D.read-only:當確定資料不會被更改時。我們可以使用這種緩衝策略。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.