带集合的hibernate查询缓存

带集合的hibernate查询缓存,hibernate,caching,Hibernate,Caching,我的数据库中有两个表,分别是Table_cities和Table_Places。表_cities是所有城市的主表,表_Places包含城市中存在的各种场所。它们与OTM关系相关联 这些表中的数据大部分是静态的,我希望将其保存在缓存中。虽然一切正常,但我想知道它是否会导致任何问题,因为我已经阅读了以下链接,查询缓存可能会对我的应用程序造成危险 请让我知道,如果我应该修改它或任何其他方式这样做 表格城市地图 <hibernate-mapping> <class name="

我的数据库中有两个表,分别是Table_cities和Table_Places。表_cities是所有城市的主表,表_Places包含城市中存在的各种场所。它们与OTM关系相关联

这些表中的数据大部分是静态的,我希望将其保存在缓存中。虽然一切正常,但我想知道它是否会导致任何问题,因为我已经阅读了以下链接,查询缓存可能会对我的应用程序造成危险

请让我知道,如果我应该修改它或任何其他方式这样做

表格城市地图

<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>
<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"
    /> 
桌面映射

<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>
<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"
    /> 
下面是我的刀

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;
    }

由于您使用只读缓存,因此可以避免链接文章第1、2和3点中提到的最多的问题。 如果这些表中的数据是有效的静态=仅插入,在hibernate启动时不进行更新/删除,那么使用只读缓存是正确的方法,根据我的经验,这一点也不危险