我正在使用Hibernate 3.2.5。
我Department
和Training
表之间有一对多的关系。启用了第二级缓存(使用EHCache),并且在andtraining.hbm.xml dept.cfg.xml
文件中都进行了以下输入以缓存数据。
<cache usage="read-only" />
Run Code Online (Sandbox Code Playgroud)
问题描述
第一次,数据库命中用于获取Dept
和Training
记录。第二次,Department
从高速缓存中获取Training
数据,但是为了获取数据,再次执行数据库命中-为什么?我希望Training
也可以从缓存中获取此数据,而不是每次都访问数据库。
这是Dept.java文件:
private int deptId;
private String deptName;
private Map trainingDetails;
Run Code Online (Sandbox Code Playgroud)
我已经在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>
Run Code Online (Sandbox Code Playgroud)
这是我尝试的代码:
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 …
Run Code Online (Sandbox Code Playgroud)