Hibernate緩存

jopen 12年前發布 | 32K 次閱讀 Hibernate 持久層框架

 

 

 

 

 

 

OSCache可以支持中央緩存

 

 

要想支持緩存hibernate.cfg.xml應該做如下配置:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    ";

<hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/LazyOfOne2One</property><!-- ///表示連接本機的數據庫//localhost:3306 --> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1234</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
     <property name="hibernate.cache.use_second_level_cache">true</property>
    <!-- 二級緩存默認是打開的,但是默認沒有提供實現類 -->
    <property name="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <!--使Query接口也支持緩存,默認是不支持的-->
    <property name="hibernate.generate_statistics">true</property>
    <!--生成統計信息-->

    <class-cache class="blog.hibernate.domain.IdCard" usage="read-write"/>
    <!--對于想要對其進行緩存的類進行配置-->

    <mapping resource="blog/hibernate/domain/Person.hbm.xml"/>
    <mapping resource="blog/hibernate/domain/IdCard.hbm.xml"/>
</session-factory>    

</hibernate-configuration></strong></pre>或者也可以在類的映射文件里進行配置(在id前加入cache即可):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        ";

<hibernate-mapping package="blog.hibernate.domain"> <class name="IdCard" table="IDCARD"> <cache usage="read-write"/> <id name="id" column="CARD_ID"> <generator class="foreign"> <param name="property">person</param> </generator> </id><!-- IdCard使用的主鍵來自主對像Person--> <property name="name" column="CARD_NAME" type="string"></property> <one-to-one name="person" constrained="true" ></one-to-one> <!-- constrained="true" 是為外鍵加約束,這在一對一中只能從對象使用,主對像是不能使用的 --> <!-- fetch 默認為 "select" --> </class> </hibernate-mapping></pre>當然還需要加入OSCache的jar包

轉自:
http://blog.csdn.net/xzf19901108/article/details/7926768

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!