新建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应用程序设计