hibernate 中属性标签的惰性属性允许根据链接延迟加载属性:http: //docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/mapping.html#mapping-declaration -财产
惰性(可选 - 默认为 false):指定在第一次访问实例变量时应该惰性地获取此属性。它需要构建时字节码检测。
但是,当我尝试为我的一个属性设置lazy=true 时,在此示例中它并没有延迟加载它:
休眠映射文件:
<hibernate-mapping package="org.hibernate.tutorial.domain">
<class name="Event" table="EVENTS" select-before-update="true">
<id name="id" column="EVENT_ID">
<generator class="native" />
</id>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" lazy="true"/>
<set name="participants" table="PERSON_EVENT" inverse="true">
<key column="EVENT_ID" />
<many-to-many column="PERSON_ID" class="Person" />
</set>
</class>
</hibernate-mapping>
程序:
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event event = (Event) session.get(Event.class, 135L);
session.getTransaction().commit();
System.out.println(event);
HibernateUtil.getSessionFactory().close();
}
hibernate生成的查询:
Hibernate: select event0_.EVENT_ID as EVENT1_0_0_, event0_.EVENT_DATE as EVENT2_0_0_, event0_.title as title0_0_ from EVENTS event0_ where event0_.EVENT_ID=?
请帮助我理解为什么懒惰在这种情况下不起作用?