我的House.hbm.mxl里
<property name="description" type="java.lang.String" lazy="true">
<column name="description" length="1000" not-null="false"></column>
</property>代码如下
String hql="from House as h where h.price=300";
List<House> houses=HibernateUtil.getSession().createQuery(hql).list();
System.out.println("获得价格为300元房屋信息");
for(House house : houses){
System.out.println("标题:"+house.getTitle());
System.out.println("描述:"+house.getDescription());
System.out.println("用户名:"+house.getUser().getName());
}
控制台的信息如下:
Hibernate: select house0_.id as id3_, house0_.type_id as type2_3_, house0_.user_id as user3_3_, house0_.street_id as street4_3_, house0_.title as title3_, house0_.description as descript6_3_, house0_.pubdate as pubdate3_, house0_.price as price3_, house0_.contact as contact3_, house0_.floorage as floorage3_ from House house0_ where house0_.price=300
获得价格为300元房屋信息
标题:挥泪价
描述:纠结哦哦
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.password as password0_0_, user0_.telephone as telephone0_0_, user0_.username as username0_0_, user0_.isadmin as isadmin0_0_ from Users user0_ where user0_.id=?
用户名:ansn001属性并没有延迟呀?????求大虾指教下。对了我的hibernate包的3.3.2
<property name="description" type="java.lang.String" lazy="true">
<column name="description" length="1000" not-null="false"></column>
</property>代码如下
String hql="from House as h where h.price=300";
List<House> houses=HibernateUtil.getSession().createQuery(hql).list();
System.out.println("获得价格为300元房屋信息");
for(House house : houses){
System.out.println("标题:"+house.getTitle());
System.out.println("描述:"+house.getDescription());
System.out.println("用户名:"+house.getUser().getName());
}
控制台的信息如下:
Hibernate: select house0_.id as id3_, house0_.type_id as type2_3_, house0_.user_id as user3_3_, house0_.street_id as street4_3_, house0_.title as title3_, house0_.description as descript6_3_, house0_.pubdate as pubdate3_, house0_.price as price3_, house0_.contact as contact3_, house0_.floorage as floorage3_ from House house0_ where house0_.price=300
获得价格为300元房屋信息
标题:挥泪价
描述:纠结哦哦
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.password as password0_0_, user0_.telephone as telephone0_0_, user0_.username as username0_0_, user0_.isadmin as isadmin0_0_ from Users user0_ where user0_.id=?
用户名:ansn001属性并没有延迟呀?????求大虾指教下。对了我的hibernate包的3.3.2
sql语句
数据
sql语句
description的语句而我没这里没有实现这个效果。
System.out.println("描述:"+house.getDescription()); 出来是很正常的
我关注的是数据在什么时候加载
正常的情况下。第一个hql语句中,不应该会出来house0_.description as descript6_3。
因为他还没加载等System.out.println("描述:"+house.getDescription());
他会出现一句sql语句的同时然后出数据。。就和下面的用户名一样加载的时候才查询数据啊???
不是数据出不出来,而是什么时候加载,优化的问题。。
<column name="description" length="1000" not-null="false"></column>
</property>另两种延迟加载我都实现了。。现在就属性加载。不知道怎么回事,实现不了?
description本来就是House的属性之一,楼主还是用String hql="from House as h where h.price=300";hql查询的,既然是hql本身属性是不支持延迟加载的,
load方法会得到你想要的结果,关联对象属性会延迟加载
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.password as password0_0_, user0_.telephone as telephone0_0_, user0_.username as username0_0_, user0_.isadmin as isadmin0_0_ from Users user0_ where user0_.id=?
用户名:ansn001
21.1.8. 使用延迟属性抓取(Using lazy property fetching)Hibernate3 对单独的属性支持延迟抓取,这项优化技术也被称为组抓取(fetch groups)。 请注意,该技术更多的属于市场特性。在实际应用中,优化行读取比优化列读取更重要。但是,仅载入类的部分属性在某些特定情况下会有用,例如在原有表中拥有几百列数据、数据模型无法改动的情况下。可以在映射文件中对特定的属性设置 lazy,定义该属性为延迟载入。
<class name="Document">
<id name="id">
<generator class="native"/>
</id>
<property name="name" not-null="true" length="50"/>
<property name="summary" not-null="true" length="200" lazy="true"/>
<property name="text" not-null="true" length="2000" lazy="true"/>
</class>
属性的延迟载入要求在其代码构建时加入二进制指示指令(bytecode instrumentation),如果你的持久类代码中未含有这些指令, Hibernate 将会忽略这些属性的延迟设置,仍然将其直接载入。你可以在 Ant 的 Task 中,进行如下定义,对持久类代码加入“二进制指令。”
<target name="instrument" depends="compile">
<taskdef name="instrument" classname="org.hibernate.tool.instrument.InstrumentTask">
<classpath path="${jar.path}"/>
<classpath path="${classes.dir}"/>
<classpath refid="lib.class.path"/>
</taskdef> <instrument verbose="true">
<fileset dir="${testclasses.dir}/org/hibernate/auction/model">
<include name="*.class"/>
</fileset>
</instrument>
</target>
还有一种可以优化的方法,它使用 HQL 或条件查询的投影(projection)特性,可以避免读取非必要的列, 这一点至少对只读事务是非常有用的。它无需在代码构建时“二进制指令”处理,因此是一个更加值得选择的解决方法。有时你需要在 HQL 中通过抓取所有属性,强行抓取所有内容。