数据库结构:

 

create table teamEH (id varchar(32),teamname varchar(32));
create table studentEH (id varchar(32),name varchar(32),team_id varchar(32));

POJO:

 

package EHCache;

public class Student {
    
private String id; //标识id
    private String name; //学生姓名
    private Team team;//班级




    
public String getName() {
        
return name;
    }


   

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


   

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


  

    
public String getId() {
        
return id;
    }


    
public Student() //无参的构造函数
    }


   

    
public Team getTeam() {
        
return team;
    }


    
public void setTeam(Team team) {
        
this.team = team;
    }

}





package EHCache;

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


public class Team {
    
private String id;
    
private Set students;
    
private String teamName;
    
public String getId() {
        
return id;
    }


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


    
public String getTeamName() {
        
return teamName;
    }


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


    
public Set getStudents() {
        
return students;
    }


    
public void setStudents(Set students) {
        
this.students = students;
    }

}

 Team.hbm.xml

其中<cache>标签表示对student集合缓存,但只缓存id,如果需要缓存student实例,则需要在student.hbm.xml中的
class标签中配置<cache>

 

<?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 - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
    
<class name="EHCache.Team" table="teamEH" lazy="false">
       
<id name="id" column="id">
         
<generator class="uuid.hex"></generator>
       
</id>
       
<property name="teamName" column="teamName"></property>
       
       
<set name="students" 
            lazy
="true" 
            inverse
="true" 
            outer-join
="false"
            batch-size
="2"
            cascade
="save-update"
           
>
           
<!-- 对students集合缓存,但只是缓存student-id如果要对整个对象缓存,
                还需要在Student.hbm.xml的class标签中加入<cache>标签 
-->
         
<cache usage="read-write"/>
         
<key column="team_id"></key>
         
<one-to-many class="EHCache.Student"/>
       
</set>
      
</class>
</hibernate-mapping>

 

Student.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 - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
   
    
<class name="EHCache.Student" table="studentEH" lazy="false">
       
<cache usage="read-write"/>
       
<id name="id" column="id" unsaved-value="null">
         
<generator class="uuid.hex"></generator>
       
</id>

       
<property name="name" column="name"></property>
    
       
<many-to-one name="team" 
                    column
="team_id"
                    outer-join
="true" 
                    cascade
="save-update"
                    class
="EHCache.Team"></many-to-one>
      
</class>
</hibernate-mapping>

 

Hibernate.cfg.xml

配置hibernate.cache.provider_class以启用EHCache

<?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="connection.username">root</property>
    
<property name="connection.url">
        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp;useUnicode=true
    
</property>
    
<property name="dialect">
        org.hibernate.dialect.MySQLDialect
    
</property>
    
<property name="myeclipse.connection.profile">mysql</property>
    
<property name="connection.password">1234</property>
    
<property name="connection.driver_class">
        com.mysql.jdbc.Driver
    
</property>
    
<property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    
</property>
    
<property name="hibernate.show_sql">true</property>
    
<property name="current_session_context_class">thread</property>

    
<property name="hibernate.cache.provider_class">
            org.hibernate.cache.EhCacheProvider
        
</property>
    
<mapping resource="EHCache/Student.hbm.xml" />
    
<mapping resource="EHCache/Team.hbm.xml" />

</session-factory>

</hibernate-configuration>

EHCache.xml(放在classpath下)

 

<ehcache>

img
评论
发表评论

您还没有登录,请登录后发表评论

esffor
搜索本博客
最近加入圈子
存档
最新评论