标签:sql 技术分享 表示 mon alter XML tran drop cache
Hibernate的二级缓存
缓存插件 | read-only | nonstrict-read-write | read-write | transaction |
EHCache | √ | √ | √ | |
OSCache | √ | √ | √ | |
SwarmCache | √ | √ | ||
JBossCache | √ | √ |
<!-- 启用二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 配置那个类使用二级缓存 -->
<class-cache usage="read-only" class="cn.hibernate3.demo4.Customer"/>
package cn.hibernate3.demo4; import java.io.Serializable; import java.util.HashSet; /** * 客户实体 */ import java.util.Set; public class Customer implements Serializable{ private Integer cid; private String cname; //一个客户有多个订单 private Set<Order> orders = new HashSet<Order>(); public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } }
package cn.hibernate3.demo4; import java.io.Serializable; /** * 订单实体 */ public class Order implements Serializable{ private Integer oid; private String addr; //订单属于某一个客户 private Customer customer ; public Integer getOid() { return oid; } public void setOid(Integer oid) { this.oid = oid; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入约束 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.hibernate3.demo4.Customer" table="customer" lazy="true"> <!-- 配置唯一标识 --> <id name="cid" column="cid"> <generator class="native"/> </id> <!-- 配置普通属性 --> <property name="cname" column="cname" type="java.lang.String"/> <!-- 建立映射 --> <!-- 配置集合 --> <!-- set标签中的name表示关联对象的属性名称 --> <set name="orders" cascade="save-update"> <!-- key标签中的column用来一对多的多的一方的外键 --> <key column="cno"/> <!-- 配置一个one-to-many --> <one-to-many class="cn.hibernate3.demo4.Order" /> </set> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入约束 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.hibernate3.demo4.Order" table="orders"> <!-- 配置唯一标识 --> <id name="oid" column="oid"> <generator class="native"/> </id> <!-- 配置普通属性 --> <property name="addr" column="addr" type="java.lang.String"/> <!-- 建立映射 --> <!-- many-to-one标签 属性: name:关联对象的属性名称。 column:表中外键的名称。 class:关联对象的全路径。 --> <many-to-one name="customer" column="cno" class="cn.hibernate3.demo4.Customer"></many-to-one> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置数据库的基本信息 --> <!-- 驱动的名称 --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 访问数据库的url --> <property name="hibernate.connection.url"> jdbc:mysql:///hibernate_day03 </property> <!-- 用户名 --> <property name="hibernate.connection.username">root</property> <!-- 密码 --> <property name="hibernate.connection.password">root</property> <!-- 方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- C3P0连接池设定--> <!-- 使用c3po连接池 配置连接池提供的供应商--> <property name="connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </property> <!--在连接池中可用的数据库连接的最少数目 --> <property name="c3p0.min_size">5</property> <!--在连接池中所有数据库连接的最大数目 --> <property name="c3p0.max_size">20</property> <!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 --> <property name="c3p0.timeout">120</property> <!--每3000秒检查所有连接池中的空闲连接 以秒为单位--> <property name="c3p0.idle_test_period">3000</property> <!-- 可选配置 --> <!-- 显示SQL --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- hbm:映射 2:to ddl:create drop alter --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 1—Read uncommitted isolation 2—Read committed isolation 4—Repeatable read isolation 8—Serializable isolation --> <property name="hibernate.connection.isolation">4</property> <property name="hibernate.current_session_context_class">thread</property> <!-- 启用二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 配置使用的二级缓存提供商 --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <mapping resource="cn/hibernate3/demo4/Customer.hbm.xml" /> <mapping resource="cn/hibernate3/demo4/Order.hbm.xml" /> <!--
<class-cache usage="read-write" class="cn.hibernate3.demo4.Customer"/>
<class-cache usage="read-write" class="cn.hibernate3.demo4.Order"/>
<collection-cache usage="read-write" collection="cn.hibernate3.demo4.Customer.orders"/>
--> </session-factory> </hibernate-configuration>
@Test public void demo13(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Customer customer1 = (Customer) session.get(Customer.class, 1); System.out.println(customer1.getCname()); tx.commit(); session = HibernateUtils.openSession(); tx = session.beginTransaction(); Customer customer2 = (Customer) session.get(Customer.class, 1); System.out.println(customer2.getCname()); tx.commit(); session.close(); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置数据库的基本信息 --> <!-- 驱动的名称 --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 访问数据库的url --> <property name="hibernate.connection.url"> jdbc:mysql:///hibernate_day03 </property> <!-- 用户名 --> <property name="hibernate.connection.username">root</property> <!-- 密码 --> <property name="hibernate.connection.password">root</property> <!-- 方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- C3P0连接池设定--> <!-- 使用c3po连接池 配置连接池提供的供应商--> <property name="connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </property> <!--在连接池中可用的数据库连接的最少数目 --> <property name="c3p0.min_size">5</property> <!--在连接池中所有数据库连接的最大数目 --> <property name="c3p0.max_size">20</property> <!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 --> <property name="c3p0.timeout">120</property> <!--每3000秒检查所有连接池中的空闲连接 以秒为单位--> <property name="c3p0.idle_test_period">3000</property> <!-- 可选配置 --> <!-- 显示SQL --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- hbm:映射 2:to ddl:create drop alter --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 1—Read uncommitted isolation 2—Read committed isolation 4—Repeatable read isolation 8—Serializable isolation --> <property name="hibernate.connection.isolation">4</property> <property name="hibernate.current_session_context_class">thread</property> <!-- 启用二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 配置使用的二级缓存提供商 --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <mapping resource="cn/hibernate3/demo4/Customer.hbm.xml" /> <mapping resource="cn/hibernate3/demo4/Order.hbm.xml" />
<!-- 配置那个类使用二级缓存-->
<class-cache usage="read-write" class="cn.hibernate3.demo4.Customer"/>
<class-cache usage="read-write" class="cn.hibernate3.demo4.Order"/>
<!-- 集合缓存区 -->
<collection-cache usage="read-write" collection="cn.hibernate3.demo4.Customer.orders"/>
</session-factory> </hibernate-configuration>
@Test public void demo13(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Customer customer1 = (Customer) session.get(Customer.class, 1); Customer customer2 = (Customer) session.get(Customer.class, 1); System.out.println(customer1==customer2); tx.commit(); session = HibernateUtils.openSession(); tx = session.beginTransaction(); Customer customer3 = (Customer) session.get(Customer.class, 1); Customer customer4 = (Customer) session.get(Customer.class, 1); System.out.println(customer3 == customer4); System.out.println(customer1 == customer3); tx.commit(); session.close(); }
@Test public void demo13(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Customer customer1 = (Customer) session.get(Customer.class, 1); System.out.println(customer1.getOrders().size()); tx.commit(); session = HibernateUtils.openSession(); tx = session.beginTransaction(); Customer custome2 = (Customer) session.get(Customer.class, 1); System.out.println(custome2.getOrders().size()); tx.commit(); session.close(); }
标签:sql 技术分享 表示 mon alter XML tran drop cache
原文地址:http://www.cnblogs.com/xuweiweiailixing/p/6778209.html