公司使用
[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 + + id)as(id))
pListadd(Projectionsproperty(alias + + name)as(name))
pListadd(Projectionsproperty(alias + + age)as(age))
pListadd(Projectionsproperty(alias + + sex)as(sex))
dcsetProjection(pList)
dcsetResultTransformer(TransformersaliasToBean(Userclass))
resultList = memberServicefindByDetached(dc)size()
String alias = user_; //查询时的table别名
DetachedCriteria dc = DetachedCriteriaforClass(Userclassalias)
ProjectionList pList = ProjectionsprojectionList()
pListadd(Projectionsproperty(alias + + id)as(id))
pListadd(Projectionsproperty(alias + + name)as(name))
pListadd(Projectionsproperty(alias + + age)as(age))
pListadd(Projectionsproperty(alias + + sex)as(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)
}