疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Hibernate只查询部分/指定字段


 

     Hibernate 使用查询部分/指定字段,有三种字现方式:

  第一种是使用高级查询DetachedCriteria实现,代码如下:

  [java]

  String alias = "user_"; //查询时的table别名

  DetachedCriteria dc = DetachedCriteria.forClass(User.class,alias);

  ProjectionList pList = Projections.projectionList();

  pList.add(Projections.property(alias + "." + "id")。as("id"));

  pList.add(Projections.property(alias + "." + "name")。as("name"));

  pList.add(Projections.property(alias + "." + "age")。as("age"));

  pList.add(Projections.property(alias + "." + "sex")。as("sex"));

  dc.setProjection(pList);

  dc.setResultTransformer(Transformers.aliasToBean(User.class));

  resultList = memberService.findByDetached(dc)。size();

  String alias = "user_"; //查询时的table别名

  DetachedCriteria dc = DetachedCriteria.forClass(User.class,alias);

  ProjectionList pList = Projections.projectionList();

  pList.add(Projections.property(alias + "." + "id")。as("id"));

  pList.add(Projections.property(alias + "." + "name")。as("name"));

  pList.add(Projections.property(alias + "." + "age")。as("age"));

  pList.add(Projections.property(alias + "." + "sex")。as("sex"));

  dc.setProjection(pList);

  dc.setResultTransformer(Transformers.aliasToBean(User.class));

  resultList = memberService.findByDetached(dc)。size();

  第二种方式是通过HQL语句new POJO()实现,方法如下:

  [java]

  package com.domain;

  public class Link {

  private String id;

  private String name;

  private String url;

  private Integer index;

  public Link(){}

  //因为:String hql = "select new Link(id,name) from Link";

  //所以必须要有接受2个参数的构造函数

  public Link(String id,String name){

  this.id = id;

  this.name = name;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this.name = name;

  }

  public String getUrl() {

  return url;

  }

  public void setUrl(String url) {

  this.url = url;

  }

  }

  package com.domain;

  public class Link {

  private String id;

  private String name;

  private String url;

  private Integer index;

  public Link(){}

  //因为:String hql = "select new Link(id,name) from Link";

  //所以必须要有接受2个参数的构造函数

  public Link(String id,String name){

  this.id = id;

  this.name = name;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this.name = name;

  }

  public String getUrl() {

  return url;

  }

  public void setUrl(String url) {

  this.url = url;

  }

  }

  通过HQL语句查询

  [java]

  String hql = "select new Link(id,name) from Link";

  Query query = session.createQuery(hql);

  //默认查询出来的list里存放的是一个Object对象,但是在这里list里存放的不再是默认的Object对象了,而是

  Link对象了

  List links = query.list();

  for(Link link : links){

  String id = link.getId();

  String name = link.getName();

  System.out.println(id + " : " + name);

  }

  String hql = "select new Link(id,name) from Link";

  Query query = session.createQuery(hql);

  //默认查询出来的list里存放的是一个Object对象,但是在这里list里存放的不再是默认的Object对象了,而是

  Link对象了

  List links = query.list();

  for(Link link : links){

  String id = link.getId();

  String name = link.getName();

  System.out.println(id + " : " + name);

  }

  第三种方式是通过HQL语句实现,类似SQL,方法如下:

  [java]

  String hql = "select id,name from Link";

  Query query = session.createQuery(hql);

  //默认查询出来的list里存放的是一个Object数组,还需要转换成对应的javaBean.

  List links = query.list();

  for(Object[] link : links){

  String id = link[0];

  String name = link[1];

  System.out.println(id + " : " + name);

  }

  String hql = "select id,name from Link";

  Query query = session.createQuery(hql);

  //默认查询出来的list里存放的是一个Object数组,还需要转换成对应的javaBean.

  List links = query.list();

  for(Object[] link : links){

  String id = link[0];

  String name = link[1];

  System.out.println(id + " : " + name);

  } 

  我们该如何学习Java呢?即使你Java零基础,但是有一颗向上好学的心,对Java感兴趣,加入疯狂Java培训班快速学习到Java核心技术,通过接受技能和经验的训练,让自己拥有一技之长,从此就业无烦恼,加入疯狂Java培训(疯狂软件学院http://www.fkjava.org/ 培训电话:020-28309358 QQ 544627560 文老师)吧,学到扎实Java技术,就业好出路!疯狂软件学院有完善的就业服务机制,学员毕业时,可以享受免费终身推荐就业服务。根据学员的特点和企业的要求,为学员推荐最合适的岗位。