Hibernate的Lazy load是很深得人心的,再配合使用Spring的OpenSessionInviewFilter基本可以減少很多勞累的工作。 Clob和Blob是兩個經常用的數據庫類型,也是查詢的殺手,在數據量上到一萬條以上,就就算你是執行一個只有十條記錄的分頁查詢,也得用上幾秒,Clob字段更加不敢用like查詢,時間長到你實在無法忍受,當然可以選擇全文檢索工具如Lucene。上面的問題自然要解決,方法可能很多,下面是其中一種,即對簡單屬性也 lazy load .
1. 編碼,對相應的hbm.xml或annotation對該屬性加上lazy=true或@Basic(fetch = FetchType.LAZY).
2. 編譯,對編譯后的Model Class進行字節碼織入,Hibernate已經提供了工具類,拿來運行即可,Ant腳本如下:
<target name="instrument">
<taskdef name="instrument" classname="org.hibernate.tool.instrument.InstrumentTask">
<classpath path="${classes.dir}"/>
<classpath refid="project.classpath"/>
</taskdef>
<instrument verbose="true">
<fileset dir="${classes.dir}/com/gdsoftpark/cms/model">
<include name="*.class"/>
</fileset>
<fileset dir="${classes.dir}/com/gdsoftpark/common/core/base">
<include name="Model.class"/>
</fileset>
</instrument>
</target>
3. 運行,從生成的SQL你可以看到某些字段確實Lazy load了。