【问题标题】:hibernate query cache with Collection使用 Collection 的休眠查询缓存
【发布时间】:2014-08-25 20:14:34
【问题描述】:

我的 Db 中有两张表,Table_cities 和 Table_Places。 Table_cities 是所有 Cities 的主表,Table_Places 包含城市中存在的各个地方。它们与 OTM 关系相关联。

这些表中的数据大部分是静态的,我想将它保存在缓存中。我已经实现了以下相同。尽管一切正常,但我想知道它是否会导致任何问题,因为我已经阅读了以下链接中的查询缓存可能对我的应用程序很危险。 http://tech.puredanger.com/2009/07/10/hibernate-query-cache/

如果我应该修改它或任何其他方式,请告诉我。

TableCities 映射

<hibernate-mapping>
    <class name="com.test.hibernateModel.Cities" table="TABLE_CITIES" schema="CAB">
     **<cache usage="read-only"/>** 
        <id name="objid" type="big_decimal">
            <column name="OBJID" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        **<natural-id>** 
        <property name="name" type="string">
            <column name="NAME" />
        </property>
        **</natural-id>**

        <set name="tablePlaces" table="TABLE_PLACES" inverse="true" lazy="true" fetch="select">
          **<cache usage="read-only" />** 
            <key>
                <column name="PLACES2CITIES"/>
            </key>
            <one-to-many class="com.test.hibernateModel.Places" />
        </set>

    </class>
</hibernate-mapping>

TablePlaces 映射

<hibernate-mapping>
    <class name="com.test.hibernateModel.Places" table="TABLE_PLACES" schema="CAB">
    **<cache usage="read-only"/>** 
        <id name="objid" type="big_decimal">
            <column name="OBJID" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="refId" type="string">
            <column name="ref_id" />
        </property>
        <property name="value" type="string">
            <column name="value" />
        </property>
      </class>
</hibernate-mapping>

ehcache.xml


     <cache name="com.test.hibernateModel.Cities"
    maxElementsInMemory="500"
    eternal="true"
    overflowToDisk="false"
    />

    <cache name="com.test.hibernateModel.Cities.tablePlaces"
    maxElementsInMemory="500"
    eternal="true"
    overflowToDisk="false"
    />

     <cache name="com.test.hibernateModel.Places"
    maxElementsInMemory="500"
    eternal="true"
    overflowToDisk="false"
    /> 

下面是我的 DAO。

public LocationsVO getLocation(String cityName) throws Exception {
        Session currentSession = null;
        try {
            currentSession = this.getSessionFactory().openSession();
            currentSession.beginTransaction();
            Criteria crit = currentSession.createCriteria(Cities.class).add(
                    Restrictions.eq("name", cityName)).setCacheable(true);
            List<Cities> cities = crit.list();
            for (int i = 0; i < cities.size(); i++) {
                location=new LocationsVO();
                Cities tempCity = cities.get(i);
                location.setCityId(tempCity.getObjid().toString());
                location.setCityName(tempCity.getName());
                location.setLocations(new ArrayList<Places>(tempCity
                        .getTablePlaces()));

            }

        } catch (Exception e) {
            throw e;
        } finally {
            if (null != currentSession)
                currentSession.close();
        }

        return location;
    }

【问题讨论】:

    标签: hibernate caching


    【解决方案1】:

    由于您使用“只读缓存”,您可以避免链接文章中提到的最常提到的问题(第 1,2 和 3 点)。 如果您在这些表中的数据是有效的静态数据(= 只有插入,在休眠状态下没有更新/删除),那么使用“只读”缓存是正确的方法,并且根据我的经验,一点也不危险。

    【讨论】:

      猜你喜欢
      • 2011-03-07
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2015-10-23
      相关资源
      最近更新 更多