waterye

hibernate的column级lazy

1. Using lazy property fetching
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>

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字段更可怕, 有其他更好方法的请告知

posted on 2006-04-10 20:28 waterye 阅读(2012) 评论(1)  编辑  收藏 所属分类: hibernate

Feedback

# re: hibernate的column级lazy 2007-04-08 21:41 rason

采用Hibernate annotation(JPA)
@Lob
@Basic(fetch=LAZY)
private byte[] fileContent;  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: