关于hibernate联级对象缓存

有部门表(PxDept)和员工表(PxEmployee)关系是PxDept(1)->PxEmployee(N)

首先PxDept.hbm.xml中这样设置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
	Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
	<class name="hibernate.bean.PxDept" table="px_dept" schema="dbo"
		catalog="test">
		<cache region="read-write"/>
		 <id name="deptId" type="integer">
			<column name="dept_id" />
			<generator class="identity" />
		</id>
		<many-to-one name="pxDept" class="hibernate.bean.PxDept"
			insert="false" update="false">
			<column name="parent_id" />
		</many-to-one>
		<property name="parentDeptId" type="integer">
			<column name="parent_id" />
		</property>
		<property name="deptName" type="string" >
			<column name="dept_name" length="256" not-null="true" />
		</property>
<set name="pxEmployees" inverse="true"  cascade="delete" batch-size="8" lazy="true">
			<cache usage="read-write"/>
			<key>
				<column name="dept_id" not-null="true" />
			</key>
			<one-to-many class="hibernate.bean.PxEmployee" />
		</set>
	</class>
</hibernate-mapping>
我使用以下查询:
(1)List list = session.createQuery("from PxDept as p1 left join fetch p1.pxEmployees").setCacheable(true).list();
(2)list = session.createQuery("from PxDept as p1 left join fetch p1.pxEmployees").setCacheable(true).list();
在(2)中的list结果是从缓存中取的,但里边PxDept对象中的关联对象pxEmployees集合是空的
下面我面又另一种方式:
将PxDept.hbm.xml中的关联关系改下
<set name="pxEmployees" inverse="true"  cascade="delete" batch-size="8" lazy="false">
			<cache usage="read-write"/>
			<key>
				<column name="dept_id" not-null="true" />
			</key>
			<one-to-many class="hibernate.bean.PxEmployee" />
		</set>
lazy="false"
(1)List list = session.createQuery("from PxDept as p1 left join fetch p1.pxEmployees").setCacheable(true).list();
(2)list = session.createQuery("from PxDept as p1 left join fetch p1.pxEmployees").setCacheable(true).list();
这会在(2)中的list 中的pxDept中的pxEmployees集合就有数据了,请问 这什么怎么回事???

 

你可能感兴趣的:(sql,Hibernate,bean,MyEclipse,cache)