如果实体对象中存在大文本或者图像等属性字段,有必要将该种字段设置为延迟加载,即在需使用该字段的时候再加载该字段的内容。
属性延迟加载的实现:
1、在实体映射文件中,通过property节点的lazy属性,可以为特定的属性制定延迟加载策略。
2、配置了lazy属性之外,还要借助类增强器对二进制Class文件进行强化处理(buildtime bytecode instrumentation)。通过ANT调用Hibernate类增强器对TUser.class文件进行强化处理。脚本如下:
属性延迟加载的实现:
1、在实体映射文件中,通过property节点的lazy属性,可以为特定的属性制定延迟加载策略。
<
hibernate-mapping
>
< class
name ="test.Tuser"
table ="T_USER"
batch-size ="5" >
< id name ="id" columen ="id" >
< generator class ="native" />
</ id >
< property
name ="name"
column ="name" />
< property
name ="age"
column ="age" />
< property
name ="resume"
column ="resume"
lazy ="true" /> //这里将个人简历(大文本字段)设置为延迟加载。
</ class >
</ hibernate-mapping >
< class
name ="test.Tuser"
table ="T_USER"
batch-size ="5" >
< id name ="id" columen ="id" >
< generator class ="native" />
</ id >
< property
name ="name"
column ="name" />
< property
name ="age"
column ="age" />
< property
name ="resume"
column ="resume"
lazy ="true" /> //这里将个人简历(大文本字段)设置为延迟加载。
</ class >
</ hibernate-mapping >
2、配置了lazy属性之外,还要借助类增强器对二进制Class文件进行强化处理(buildtime bytecode instrumentation)。通过ANT调用Hibernate类增强器对TUser.class文件进行强化处理。脚本如下:
<
project
name
="HibernateSample"
default
="instrument"
basedir
="."
>
< property name ="lib.dir" value ="./lib" />
< property name ="classes.dir" value ="./bin" />
< path id ="lib.class.path" >
< fileset dir ="${lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
< target name ="instrument" >
< taskdef name ="instrument"
classname ="org.hibernate.tool.instrument.InstrumentTask" >
< classpath path ="${classes.dir}" />
< classpath refid ="lib.class.path" />
</ taskdef >
< instrument verbose ="true" >
< fileset dir ="${classes.dir}/com.redsaga/hibernate/db/entity" >
< include name ="TUser.class" />
</ fileset >
</ instrument >
</ target >
</ project >
注:脚本中涉及的配置路径,根据项目目录修改。
< property name ="lib.dir" value ="./lib" />
< property name ="classes.dir" value ="./bin" />
< path id ="lib.class.path" >
< fileset dir ="${lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
< target name ="instrument" >
< taskdef name ="instrument"
classname ="org.hibernate.tool.instrument.InstrumentTask" >
< classpath path ="${classes.dir}" />
< classpath refid ="lib.class.path" />
</ taskdef >
< instrument verbose ="true" >
< fileset dir ="${classes.dir}/com.redsaga/hibernate/db/entity" >
< include name ="TUser.class" />
</ fileset >
</ instrument >
</ target >
</ project >