Hibernate二级缓存存集合对象

新建java project项目:chapter17_setehcache

hibernate.cfg.xml

代码:

<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 <!-- Generated by MyEclipse Hibernate Tools.                   --> <hibernate-configuration>

 <session-factory>   <property name="dialect">    org.hibernate.dialect.MySQLDialect   </property>   <property name="connection.url">    jdbc:mysql://localhost:3307/users   </property>   <property name="connection.username">root</property>   <property name="connection.password">root</property>   <property name="connection.driver_class">    com.mysql.jdbc.Driver   </property>   <property name="myeclipse.connection.profile">    mysqlusers   </property>   <property name="show_sql">true</property>   <property name="format_sql">true</property>   <property name="current_session_context_class">thread</property>   <property name="cache.provider_class">    org.hibernate.cache.EhCacheProvider      </property>   <mapping resource="com/b510/examplex/Category.hbm.xml" />   <mapping resource="com/b510/examplex/Product.hbm.xml" />

 </session-factory>

</hibernate-configuration>

ehcache.xml

代码:

<?xml version="1.0" encoding="UTF-8"?> <ehcache>              <!-- 说明:maxElementsInMemory  设置 保存在内存中的缓存对象的最大数量                etemal  设置缓存中对象 是否永远不过期,如果值为true,超过设置被忽略,缓存对象永远不过期                timeToIdleSeconds   设置缓存中对象在他过期之前的最大空闲时间,单位为秒                timeToLiveSeconds   设置缓存中对象在他过期之前的最大生存时间 ,单位为秒                overflowToDisk      设置内存中的缓存对象达到maxElementsInMemory限制时,是否将缓存对象保存到硬盘中                    -->      <!--  -->  <diskStore path="java.io.tmpdir"/>  <defaultCache maxElementsInMemory="10000" eternal="false"  timeToIdleSeconds="120" timeToLiveSeconds="120"  overflowToDisk="true"/>  <cache name="com.b510.examples.Product" maxElementsInMemory="1000"  eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600"  overflowToDisk="true"/>   <cache name="com.b510.example.Category.products" maxElementsInMemory="1000"  eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600"  overflowToDisk="true"/>   <cache name="com.b510.example.Category" maxElementsInMemory="1000"  eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600"  overflowToDisk="true"/>  </ehcache>

Category.java

代码:

package com.b510.examplex;

import java.util.HashSet; import java.util.Set;

/**  * Category entity. @author MyEclipse Persistence Tools  */

public class Category implements java.io.Serializable {

 private static final long serialVersionUID = 7187702390542761264L;  private Integer id;  private String name;  private String description;  private Set products = new HashSet(0);

 public Category() {  }

 public Integer getId() {   return this.id;  }

 public void setId(Integer id) {   this.id = id;  }

 public String getName() {   return this.name;  }

 public void setName(String name) {   this.name = name;  }

 public String getDescription() {   return this.description;  }

 public void setDescription(String description) {   this.description = description;  }

 public Set getProducts() {   return this.products;  }

 public void setProducts(Set products) {   this.products = products;  }

}

Product.java

代码;

package com.b510.examplex;

/**  * Product entity. @author MyEclipse Persistence Tools  */

public class Product implements java.io.Serializable {

 private static final long serialVersionUID = 1449100306422352085L;  private Integer id;  private Category category;  private String name;  private String price;  private String descripton;

 public Product() {  }

 public Integer getId() {   return this.id;  }

 public void setId(Integer id) {   this.id = id;  }

 public Category getCategory() {   return this.category;  }

 public void setCategory(Category category) {   this.category = category;  }

 public String getName() {   return this.name;  }

 public void setName(String name) {   this.name = name;  }

 public String getPrice() {   return this.price;  }

 public void setPrice(String price) {   this.price = price;  }

 public String getDescripton() {   return this.descripton;  }

 public void setDescripton(String descripton) {   this.descripton = descripton;  }

}

Category.hbm.xml

代码:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!--     Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping>     <class name="com.b510.examplex.Category" table="category" catalog="users">      <cache usage="read-only"/>         <id name="id" type="java.lang.Integer">             <column name="id" />             <generator class="increment"></generator>         </id>         <property name="name" type="java.lang.String">             <column name="name" length="500" />         </property>         <property name="description" type="java.lang.String">             <column name="description" length="500" />         </property>         <set name="products" inverse="true">       <cache usage="read-only"/>             <key>                 <column name="category_id" />             </key>             <one-to-many class="com.b510.examplex.Product" />         </set>     </class> </hibernate-mapping>

Product.hbm.xml

代码:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!--     Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping>     <class name="com.b510.examplex.Product" table="product" catalog="users">         <id name="id" type="java.lang.Integer">             <column name="id" />             <generator class="increment"></generator>         </id>         <many-to-one name="category" class="com.b510.examplex.Category" fetch="select">             <column name="category_id" />         </many-to-one>         <property name="name" type="java.lang.String">             <column name="name" length="500" />         </property>         <property name="price" type="java.lang.String">             <column name="price" length="10" />         </property>         <property name="descripton" type="java.lang.String">             <column name="descripton" length="500" />         </property>     </class> </hibernate-mapping>

测试代码:

HibernateTest.java

代码:

/**  *  */ package com.b510.examplex;

import java.util.Set;

import org.hibernate.Session;

/**  *  * @author XHW  *  * @date 2011-7-17  *  */ public class HibernateTest {  public static void main(String[] args) {   new HibernateTest().test();  }  public void test(){   Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session.beginTransaction();   Category c1=(Category)session.get(Category.class, 1);   System.out.println("id:"+c1.getId()+"  name: "+c1.getName());   Set<Product> products=c1.getProducts();   for(Product p:products){    System.out.println(p.getName());   }     session.getTransaction().commit();   System.out.println("--------------------------------");   Session session2=HibernateSessionFactoryUtil.getSessionFactory().openSession();   session2.beginTransaction();   Category c2=(Category)session2.get(Category.class, 1);   Set<Product> products2=c1.getProducts();   for(Product p2:products2){    System.out.println(p2.getName());   }     session2.getTransaction().commit();  } }

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Hibernate:     select         category0_.id as id0_0_,         category0_.name as name0_0_,         category0_.description as descript3_0_0_     from         users.category category0_     where         category0_.id=? id:1  name: java Hibernate:     select         products0_.category_id as category2_1_,         products0_.id as id1_,         products0_.id as id1_0_,         products0_.category_id as category2_1_0_,         products0_.name as name1_0_,         products0_.price as price1_0_,         products0_.descripton as descripton1_0_     from         users.product products0_     where         products0_.category_id=? java WEB开发与实战 java SE应用程序设计 -------------------------------- java WEB开发与实战 java SE应用程序设计

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏扎心了老铁

Hadoop通过HCatalog编写Mapreduce任务访问hive库中schema数据

1、dirver package com.kangaroo.hadoop.drive; import java.util.Map; import java.u...

73750
来自专栏ml

HDUOJ---What Are You Talking About

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Lim...

397140
来自专栏Hongten

Hibernate HQL 语句

import java.util.Iterator; import java.util.List; import java.util.Map;

21620
来自专栏Hongten

Hibernate EntityManager

Java Persistence API(JPA)是EJB3.0规范之一,定义了对数据库数据进行持久化操作的接口。HIbernate

14240
来自专栏函数式编程语言及工具

Akka(40): Http:Marshalling reviewed - 传输数据序列化重温

   上篇我们讨论了Akka-http的文件交换。由于文件内容编码和传输线上数据表达型式皆为bytes,所以可以直接把文件内容存进HttpEntity中进行传递...

24780
来自专栏测试开发架构之路

注解与依赖注入

1.Product package com.how2java.pojo; import org.springframework.beans.factory.a...

30460
来自专栏ml

hduoj------2594 Simpsons’ Hidden Talents

Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:...

27340
来自专栏Google Dart

AOP切面编程三 原

6720
来自专栏闻道于事

JavaWeb之ssm框架整合,用户角色权限管理

SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring ...

2.5K40
来自专栏用户2442861的专栏

Good logging practice in Python

https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/

15710

扫码关注云+社区

领取腾讯云代金券