【发布时间】: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;
}
【问题讨论】: