最近在做一个项目,项目对应的数据里面有一个table为teacher
项目中有一个实体类为Teacher,ORM文件为:Teacher.hbm.xml
实体类Teacher[code=java]public class _Teacher {
private int tid;
private String tname;
private int tsex;
private int tage;
private Set<_Class> _class;
public _Teacher() {
_class = new HashSet<_Class>();
}
//...省略getter、setter方法
}[/code]Teacher类的映射文件:[code=java]//...省略头部信息
<hibernate-mapping package="com.test.model">
<class name="_Teacher" table="_teacher">
<id name="tid" column="tid">
<generator class="native"></generator>
</id>
<property name="tname" type="java.lang.String" lazy="true">
<column name="tname" length="20" not-null="true"></column>
</property>
<property name="tsex" type="java.lang.Integer">
<column name="tsex" not-null="true"></column>
</property>
<property name="tage" type="java.lang.Integer">
<column name="tage" not-null="true"></column>
</property>
<set name="_class" table="_class" cascade="delete">
<key>
<column name="tid"/>
</key>
<one-to-many class="com.test.model._Class"/>
</set>
</class>
</hibernate-mapping>[/code]在映射文件中,我将tname属性的lazy="true"[code=java] String hql = "from _Teacher";
Query q = session.createQuery(hql);
List<_Teacher> lc = q.list();
for(_Teacher t : lc) {
System.out.println("sex:"+t.getTsex());
System.out.println("name:"+t.getTname());
}[/code]输出为:
Hibernate: select teacher0_.tid as tid0_, teacher0_.tname as tname0_, teacher0_.tsex as tsex0_, teacher0_.tage as tage0_ from _teacher teacher0_
sex:0
name:accp
sex:0
name:李思文
Hibernate将所有表中信息加载出来了,而我设置了tname属性延迟加载,照理说在输出t.getTname()的时候session应该执行查询操作啊,但是却没有。这让我百思不得其解。找了半天也没找出问题症结所在。有高人来解释下吗?
[ 本帖最后由 许晨峰 于 2011-09-27 19:38 编辑 ] |