一对多协会的二级缓存 [英] Second level caching for One To Many Associations

查看:104
本文介绍了一对多协会的二级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate 3.2.5。



我有部门 code> Training 表。启用第二级缓存(使用EHCache),并在 dept.cfg.xml 和`training.hbm.xml文件中创建用于缓存数据的以下条目。

 < cache usage =read-only/> 

问题说明



第一次,DB命中是为了获得 Dept Training 记录。第二次,从缓存中提取 Department 数据,但是为了获得 Training 数据,完成了DB命中再次 - 为什么?我希望这个训练数据也可以从缓存中获取,而不是每次都击中数据库。



Dept.java 档案:

  private int deptId; 
private String deptName;
private地图训练详情;

我已经提到 dept.hbm.xml 文件中的映射如下:

  // POJO和DB之间的映射表
< map name =trainingDetailsinverse = cascade =deletelazy =false>
< key column =DEPT_ID>< / key>
< map-key formula =IDtype =integer>< / map-key>
< one-to-many class =com.model.Training/>
< / map>

这是我尝试的代码:

  SessionFactory sf = new Configuration()。configure(trial.cfg.xml)。buildSessionFactory(); 
会话session = sf.openSession();

部门=(Dept)session.load(Dept.class,1);
//一些业务相关的操作
session.flush();
session.close();
//有些操作
会话session1 = sf.openSession();
Dept department1 =(Dept)session1.load(Dept.class,1);
//这里我可以看到在控制台获取
//训练细节的部门的查询再次执行
//但部门详细信息正在从缓存获取 - 为什么?
//我想要的培训细节也从缓存中获取。
session1.flush();
session1.close();

请告诉我我缺少什么以及如何解决这个问题。

解决方案

如果你告诉Hibernate在第二级缓存中缓存部门实体, c $ c> Department ,它将存储 deptId deptName 字段的值。但是,它不会默认存储 trainingDetails 字段的内容。如果从二级缓存读取 Department ,并且应用程序需要访问成员字段,Hibernate将转到数据库以确定集合的当前成员。 p>

如果你希望Hibernate缓存成员字段的内容,你需要通过添加一个 cache 元素到成员声明:

  // POJO和DB Table 
< map name =trainingDetailsinverse =falsecascade =deletelazy =false>
<! - 实体的ID是此集合的成员 - >
< cache usage =read-only/>
< key column =DEPT_ID>< / key>
< map-key formula =IDtype =integer>< / map-key>
< one-to-many class =com.model.Training/>
< / map>


I am using Hibernate 3.2.5.

I have a one to many relation between Department and Training tables. The second-level caching is enabled (Using EHCache) and the below entry is made in both the dept.cfg.xml and `training.hbm.xml files for caching the data.

<cache usage="read-only" />

Problem Description

For the first time, DB hit is done for getting both the Dept and Training records. The second time, the Department data is fetched from the cache but for getting the Training data, a DB hit is done again - WHY? I want this Training data also to be fetched from the cache rather than hitting the DB every time.

This is Dept.java file:

private int deptId;
private String deptName;
private Map trainingDetails;

I have mentioned the mapping in the dept.hbm.xml file as follows:

//Mappings between POJO and DB Table
<map name="trainingDetails" inverse="false" cascade="delete" lazy="false">
      <key column="DEPT_ID"></key>          
      <map-key formula="ID" type="integer"></map-key>
      <one-to-many class="com.model.Training"/>          
</map>

This is the code I tried:

SessionFactory sf = new Configuration().configure("trial.cfg.xml").buildSessionFactory();
    Session session = sf.openSession();

    Dept department = (Dept)session.load(Dept.class, 1);
    //Some business related operations
    session.flush();
    session.close();
            //Some operations
            Session session1 = sf.openSession();        
    Dept department1 = (Dept)session1.load(Dept.class, 1);
    //Here I can see in the console the query for fetching the 
    //training details for the department is getting executed again
    //but the Department details is getting fetched from the Cache - WHY?
    //I want the Training details also to be fetched from the cache.
    session1.flush();
    session1.close();

Kindly let me know what am I missing and how to resolve this.

解决方案

If you tell Hibernate to cache Department entities in the second level cache, for each cached Department it will store the values for the deptId and deptName fields. However, it doesn’t by default store the contents of the trainingDetails field. If a Department is read from the second level cache and the application needs to access the members field, Hibernate will go to the database to determine the current members of the collection.

If you want Hibernate to cache the contents of the members field, you need to tell it to do so by adding a cache element to the members declaration:

//Mappings between POJO and DB Table
<map name="trainingDetails" inverse="false" cascade="delete" lazy="false">
      <!-- Cache the ids of entities are members of this collection -->
      <cache usage="read-only" />
      <key column="DEPT_ID"></key>          
      <map-key formula="ID" type="integer"></map-key>
      <one-to-many class="com.model.Training"/>          
</map>

这篇关于一对多协会的二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆