OSCache可以支持中央缓存
要想支持缓存hibernate.cfg.xml应该做如下配置:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<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>
或者也可以在类的映射文件里进行配置(在id前加入cache即可):
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<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>
当然还需要加入OSCache的jar包