1. Using lazy property fetching
To enable lazy property loading, set the lazy attribute on your particular property mappings:
Lazy property loading requires buildtime bytecode instrumentation! If your persistent classes are not enhanced, Hibernate will silently ignore lazy property settings and fall back to immediate fetching.
For bytecode instrumentation, use the following 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
>
//
use vo
String hql
=
"
select new Foo(f.id, f.name) from Foo f
"
;

//
use map
String hql
=
"
select new map(f.id, f.name) from Foo f
"
;

//
use Object[]
String hql
=
"
select f.id, f.name from Foo f
"
;

//
use list
String
"
select new list(f.id, f.name) from Foo f
"
;
不支持嵌套的对象, 不爽, 如
String hql
=
"
select new Foo(f.id, new Bar(f.bar.id, f.bar.name)) from Foo f
"
;
以上两种方法在实际应用中都不是很理想, 但那种from Pojo的方式太浪费内存, 遇到 blob字段更可怕, 有其他更好方法的请告知
To enable lazy property loading, set the lazy attribute on your particular property mappings:
<
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 >
< 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 >
Lazy property loading requires buildtime bytecode instrumentation! If your persistent classes are not enhanced, Hibernate will silently ignore lazy property settings and fall back to immediate fetching.
For bytecode instrumentation, use the following 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
>
Please note that this is mostly a marketing feature, as in practice, optimizing row reads is much more important than optimization of column reads.
debug麻烦, 并没有测试
2. use hql
//
use vo
String hql
=
"
select new Foo(f.id, f.name) from Foo f
"
;
//
use map
String hql
=
"
select new map(f.id, f.name) from Foo f
"
;
//
use Object[]
String hql
=
"
select f.id, f.name from Foo f
"
;
//
use list
String
"
select new list(f.id, f.name) from Foo f
"
;
String hql
=
"
select new Foo(f.id, new Bar(f.bar.id, f.bar.name)) from Foo f
"
;
以上两种方法在实际应用中都不是很理想, 但那种from Pojo的方式太浪费内存, 遇到 blob字段更可怕, 有其他更好方法的请告知
本文介绍如何在Hibernate中配置懒加载属性,并通过HQL进行数据查询。懒加载能够提高应用程序性能,减少不必要的数据库交互。文章还探讨了使用HQL进行不同类型的数据选择的方法。
2132

被折叠的 条评论
为什么被折叠?



