简体   繁体   English

一对多关联的二级缓存

[英]Second level caching for One To Many Associations

I am using Hibernate 3.2.5. 我正在使用Hibernate 3.2.5。

I have a one to many relation between Department and Training tables. 我在Department表和Training表之间存在一对多的关系。 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. 启用了第二级缓存(使用EHCache),并且在dept.cfg.xml和`training.hbm.xml文件中都进行了以下输入以缓存数据。

<cache usage="read-only" />

Problem Description 问题描述

For the first time, DB hit is done for getting both the Dept and Training records. 第一次,数据库命中获得了DeptTraining记录。 The second time, the Department data is fetched from the cache but for getting the Training data, a DB hit is done again - WHY? 第二次,从高速缓存中获取Department数据,但是为了获取Training数据,再次执行了数据库命中-为什么? I want this Training data also to be fetched from the cache rather than hitting the DB every time. 我希望也可以从缓存中获取此Training数据,而不是每次都访问数据库。

This is Dept.java file: 这是Dept.java文件:

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

I have mentioned the mapping in the dept.hbm.xml file as follows: 我已经在dept.hbm.xml文件中提到了映射,如下所示:

//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. 如果告诉Hibernate在第二级缓存中缓存Department实体,则对于每个缓存的Department ,它将存储deptIddeptName字段的值。 However, it doesn't by default store the contents of the trainingDetails field. 但是,默认情况下,它不存储trainingDetails字段的内容。 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. 如果从第二级缓存中读取了Department ,并且应用程序需要访问member字段,则Hibernate将转到数据库以确定集合的当前成员。

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: 如果您希望Hibernate缓存member字段的内容,则需要通过在members声明中添加一个cache元素来告诉它这样做:

//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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM