熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate只查詢部分/指定字段

2013-11-23 20:38:18  來源: Java開源技術 

  公司使用

  [java]

  DetachedCriteria detachedCriteria = DetachedCriteriaforClass(PeBulletinclass)

  detachedCriteriacreateAlias(enumConstByFlagIsvalid enumConstByFlagIsvalid

  detachedCriteriacreateCriteria(peSite peSite

  detachedCriteriacreateCriteria(peManager peManager

  detachedCriteriaadd(Restrictionseq(enumCode ))

  detachedCriteriacreateAlias(enumConstByFlagIstop enumConstByFlagIstop

  detachedCriteriaaddOrder(Orderdesc(enumde))addOrder(Orderdesc(publishDate))

  DetachedCriteria detachedCriteria = DetachedCriteriaforClass(PeBulletinclass)

  detachedCriteriacreateAlias(enumConstByFlagIsvalid enumConstByFlagIsvalid

  detachedCriteriacreateCriteria(peSite peSite

  detachedCriteriacreateCriteria(peManager peManager

  detachedCriteriaadd(Restrictionseq(enumCode ))

  detachedCriteriacreateAlias(enumConstByFlagIstop enumConstByFlagIstop

  detachedCriteriaaddOrder(Orderdesc(enumde))addOrder(Orderdesc(publishDate))的QBC方式查詢數據

  這種方式的最大好處是幾乎完全面向對象是一種在HQL更上層的對象封裝了幾乎讓你忘記SQL是什麼玩意了

  但是他有一個最大的弊端就是效率問題默認他會查詢這個對象的所有字段【包括它的關聯對象】像我上面這個查詢最後查出來的字段起碼有多個字段了效率問題可想而知實在不想去寫SQL或HQL太麻煩了於是想到將其進行優化的最好方法

  果然QBC提供了字段的部分查詢也許hibernate的作者說得對說hibernate效率低只能說你還不懂怎麼去用雖然這家伙在寫hibernate之前不懂SQL它的二級緩存做得不錯

  言歸正傳

  Hibernate 使用查詢部分/指定字段有三種字現方式

  第一種是使用高級查詢DetachedCriteria實現代碼如下

  [java]

  String alias = user_; //查詢時的table別名

  DetachedCriteria dc = DetachedCriteriaforClass(Userclassalias)

  ProjectionList pList = ProjectionsprojectionList()

  pListadd(Projectionsproperty(alias + + idas(id))

  pListadd(Projectionsproperty(alias + + nameas(name))

  pListadd(Projectionsproperty(alias + + ageas(age))

  pListadd(Projectionsproperty(alias + + sexas(sex))

  dcsetProjection(pList)

  dcsetResultTransformer(TransformersaliasToBean(Userclass))

  resultList = memberServicefindByDetached(dc)size()

  String alias = user_; //查詢時的table別名

  DetachedCriteria dc = DetachedCriteriaforClass(Userclassalias)

  ProjectionList pList = ProjectionsprojectionList()

  pListadd(Projectionsproperty(alias + + idas(id))

  pListadd(Projectionsproperty(alias + + nameas(name))

  pListadd(Projectionsproperty(alias + + ageas(age))

  pListadd(Projectionsproperty(alias + + sexas(sex))

  dcsetProjection(pList)

  dcsetResultTransformer(TransformersaliasToBean(Userclass))

  resultList = memberServicefindByDetached(dc)size()

  第二種方式是通過HQL語句new POJO()實現方法如下

  [java]

  package comdomain;

  public class Link {

  private String id;

  private String name;

  private String url;

  private Integer index;

  public Link(){}

  //因為String hql = select new Link(idname) from Link;

  //所以必須要有接受個參數的構造函數

  public Link(String idString name){

  thisid = id;

  thisname = name;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  thisname = name;

  }

  public String getUrl() {

  return url;

  }

  public void setUrl(String url) {

  thisurl = url;

  }

  }

  package comdomain;

  public class Link {

  private String id;

  private String name;

  private String url;

  private Integer index;

  public Link(){}

  //因為String hql = select new Link(idname) from Link;

  //所以必須要有接受個參數的構造函數

  public Link(String idString name){

  thisid = id;

  thisname = name;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  thisname = name;

  }

  public String getUrl() {

  return url;

  }

  public void setUrl(String url) {

  thisurl = url;

  }

  }

  通過HQL語句查詢

  [java]

  String hql = select new Link(idname) from Link;

  Query query = sessioncreateQuery(hql)

  //默認查詢出來的list裡存放的是一個Object對象但是在這裡list裡存放的不再是默認的Object對象了而是Link對象了

  List<Link> links = querylist()

  for(Link link : links){

  String id = linkgetId()

  String name = linkgetName()

  Systemoutprintln(id + : + name)

  }

  String hql = select new Link(idname) from Link;

  Query query = sessioncreateQuery(hql)

  //默認查詢出來的list裡存放的是一個Object對象但是在這裡list裡存放的不再是默認的Object對象了而是Link對象了

  List<Link> links = querylist()

  for(Link link : links){

  String id = linkgetId()

  String name = linkgetName()

  Systemoutprintln(id + : + name)

  }

  第三種方式是通過HQL語句實現類似SQL方法如下

  [java]

  String hql = select idname from Link;

  Query query = sessioncreateQuery(hql)

  //默認查詢出來的list裡存放的是一個Object數組還需要轉換成對應的javaBean

  List<Object[]> links = querylist()

  for(Object[] link : links){

  String id = link[];

  String name = link[];

  Systemoutprintln(id + : + name)

  }

  String hql = select idname from Link;

  Query query = sessioncreateQuery(hql)

  //默認查詢出來的list裡存放的是一個Object數組還需要轉換成對應的javaBean

  List<Object[]> links = querylist()

  for(Object[] link : links){

  String id = link[];

  String name = link[];

  Systemoutprintln(id + : + name)

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28800.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.