eagle

学无止境,细节决定成败.
posts - 12, comments - 11, trackbacks - 0, articles - 2
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

hibernate 实体字段延迟加载简单处理方式

Posted on 2009-07-16 17:46 月下孤城 阅读(2251) 评论(0)  编辑  收藏 所属分类: hibernate

hibernate中对字段的延迟加载一般通过两种方式处理:
    1.hibernate3提供对实体对象字段延迟加载属性(lazy=true/false)设置,并借助类增强器对二进制Class文件进行强化处理,然后就可以像使用set集合一样使用字段的延迟加载。
详细请参考:http://www.blogjava.net/wiflish/archive/2006/08/15/63692.html

    2.通过实体对象的粒度细分来来实现。
详细请参考:http://dingjun1.javaeye.com/blog/184082

但以上实现都很麻烦。对字段的延迟加载总觉得没set结合的延迟来的简单。换个思考方式,在加载实体对象的时候如果只加载需要的字段属性,不查询延迟加载的字段,同样达到提高效率的目的。只是这样在返回的结果列表对象类型却为Object数组,处理结果集时又和jdbc编程有点相似了,感觉有点怪怪的^_^(好像hibnernate对象映射优势没显示出来),仔细的查了下资料,其实hibernate select中支持new 关键词查询,在执行查询后她会把select的字段属性值根据构造器的反射原理自动注入到new 的对象属性中。

实例演示:
1.实体类:其中faqContent数据库中类型varcher(8000).

public class Faqcontent implements 
java.io.Serializable
{
    
private String     faqcntId;

    
private Faqcatalog faqcatalog;//目录id

    
private String     faqcntTitle;//title

    
private String     faqcntConent;//内容字段,varchar(8000)字符

public Faqcontent()
    
{
    }

    
    
/**
     * 
@param faqcntId
     * 
@param faqcatalog
     * 
@param faqcntTitle
     */

    
public Faqcontent(String faqcntId, Faqcatalog faqcatalog,
            String faqcntTitle)
    
{
        
super();
        
this.faqcntId = faqcntId;
        
this.faqcatalog = faqcatalog;
        
this.faqcntTitle = faqcntTitle;
       }


.set
/get方法
}

2.Service中部分代码:
1public List<Faqcontent> getPageOfFaqcontentList(int pageSize,int recordPerPage,String condition,List paramValues){
22           StringBuilder hqlSb = new StringBuilder();
3                 hqlSb.append(" select new Faqcontent(faqc.faqcntId,faqc.faqcatalog,faqc.faqcntTitle)   from faqcontent faqc ");
4                  .
53}

说明:在组织hql语句"select new Faqcontent(faqc.faqcntId,faqc.faqcatalog,faqc.faqcntTitle) from faqcontent faq "时,对应的实体对象Faqcontent一定要有相应的构造方法。


---------------------
月下孤城
mail:eagle_daiqiang@sina.com

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


网站导航: